How to use if condition in my wordpress shortcode

I want to use a condition with shortcode.

How can I use if else condition from shortcode?

Read More

Here is my code :

    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');

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

Related posts

Leave a Reply

1 comment

  1. I think you are trying to check client name and company name if they are given from the back end. These fields are optional and you need to show those fields in your page only if there are some values there.

    Please check with this code. This should help you:

    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!