• Resolved Belal Khan

    (@belalkhan2292)


    i want to use a condition with shortcode.

    how can i use if else condition from shortcode. ?

    Here is my code :

    [ Moderator note: code fixed. Please wrap code in the backtick character or use the code button. ]

    /*-----Query post in shortcode for testimonial content----*/
    
    function testimonial_content_shortcode($atts){
      extract( shortcode_atts( array(
        'category' => '',
        'count' => '',
        'type' => 'post',
      ), $atts  ) );
    
        $q = new WP_Query(
            array('posts_per_page' => $count, 'post_type' => 'testimonial-items', 'order', 'ASC' )
            );    
    
      $list = '<div class="col-md-6 wow fadeIn belal_all_testimonial" data-wow-duration="0.6s" data-wow-delay="0.3s">';
    
      while($q->have_posts()) : $q->the_post();
         $idd = get_the_ID();
    	 $client_name = get_post_meta($idd, 'client_name', true);
    	 $company_name = get_post_meta($idd, 'company_name', true);
    
        $list .= '
    
                <div id="testimonial-'.$idd.'" class="testimonail-detail">
                <p>
                '.get_the_content().'
                </p>
                <div class="testimonial-info">
    
                  <span class="company">
                  Client Name:
                </span>
    
                  <span class="name">
                     '.$client_name.'
                  </span>
    
                  <span class="company">
                  Company Name:
                </span>
                  <span class="name">
                     '.$company_name.'
                  </span>
                </div>
              </div>
    
        ';
      endwhile;
      $list.= '</div>';
      wp_reset_query();
      return $list;
    }
    add_shortcode('tcontent', 'testimonial_content_shortcode');

    LOOK MY CODE . i want when i will put my client name then it will be show in my page Client Name: Belal . But when i will not select any name that’s why it will not show on My client name:

    NB: i use option tree

    Thanks for your reading in my Thread

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    What condition are you looking for in your short code?

    If I get that right, if $client_name is empty (empty string), you don’t want to show anything, right? If yes, it should be enough to add one if condition in the while loop, such as this:

    while($q->have_posts()) : $q->the_post();
        $idd = get_the_ID();
    
        $client_name = get_post_meta($idd, 'client_name', true);
    
        if (empty($client_name)) {
            return;
        }
    
        $company_name = get_post_meta($idd, 'company_name', true);

    Thread Starter Belal Khan

    (@belalkhan2292)

    Hey lamosty,

    Thanks for your comment .

    But i have a question to you that where i use

    if (empty($client_name)) {
    return;
    }

    Below the my code. please edit it and send me resolve code please .

    while($q->have_posts()) : $q->the_post();
    $idd = get_the_ID();
    $client_name = get_post_meta($idd, ‘client_name’, true);
    $company_name = get_post_meta($idd, ‘company_name’, true);

    $list .= ‘

    <div id=”testimonial-‘.$idd.'” class=”testimonail-detail”>
    <p>
    ‘.get_the_content().’
    </p>
    <div class=”testimonial-info”>

    <span class=”company”>
    Client Name:
    </span>

    <span class=”name”>
    ‘.$client_name.’
    </span>

    <span class=”company”>
    Company Name:
    </span>
    <span class=”name”>
    ‘.$company_name.’
    </span>
    </div>
    </div>

    Thread Starter Belal Khan

    (@belalkhan2292)

    Hello Jan Dembowski

    i am, looking for if else condition for shortcode .

    Hi belalkhan2292,

    You can try with this code.

    /*-----Query post in shortcode for testimonial content----*/
    
    function testimonial_content_shortcode($atts){
      extract( shortcode_atts( array(
        'category' => '',
        'count' => '',
        'type' => 'post',
      ), $atts  ) );
    
        $q = new WP_Query(
            array('posts_per_page' => $count, 'post_type' => 'testimonial-items', 'order', 'ASC' )
            );    
    
      $list = '<div class="col-md-6 wow fadeIn belal_all_testimonial" data-wow-duration="0.6s" data-wow-delay="0.3s">';
    
      while($q->have_posts()) : $q->the_post();
         $idd = get_the_ID();
    	 $client_name = get_post_meta($idd, 'client_name', true);
    	 $company_name = get_post_meta($idd, 'company_name', true);
    
        $list .= '
    
                <div id="testimonial-'.$idd.'" class="testimonail-detail">
                <p>
                '.get_the_content().'
                </p>
                <div class="testimonial-info">';
    
    if( !empty($client_name) ) {
    
             $list .= '<span class="company">
                  Client Name:
                </span><span class="name">
                     '.$client_name.'
                  </span>';
    }
    if( !empty($company_name) ) {
             $list .= '<span class="company">
                  Company Name:
                </span>
                  <span class="name">
                     '.$company_name.'
                  </span>';
    }
    
    $list .= '</div>
              </div>';
    
      endwhile;
      $list.= '</div>';
      wp_reset_query();
      return $list;
    }
    add_shortcode('tcontent', 'testimonial_content_shortcode');

    Thanks!

    Thread Starter Belal Khan

    (@belalkhan2292)

    Hi arefin2k,
    your Solution is Awesome. Thanks for your solution.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Shortcode Condition’ is closed to new replies.