How can i get first image if the post has no post thumbnail in wordpress

I have created the sidebar widget for popular, recent and most commented posts in my theme. I have some posts which don’t contain the image thumbnail.
This is the popular query posts for 5 posts in my widget

<?php if (!empty($popular_posts)) { ?>
            <div class="tab-pane fade in active" id="popular">
                <div class="row">

                <!-- ********************************************** -->
                <!-- Popular Posts Tab -->
                <!-- ********************************************** -->
                <?php
                    $YPE_options = get_option( 'YPE_sidebar_option_name' ); 
                    $popular = new WP_Query( array( 
                        'posts_per_page' => $popular_limit, 
                        'meta_key' => 'post_views_count', // this is function within functions.php for counting post veiews
                        'orderby' => 'meta_value_num', 
                        'order' => 'DESC'  
                    ));
                while ( $popular->have_posts() ) : $popular->the_post();
                    $html  = '<article>';
                    $html .= '<section class="bootstrap-nav-thumb">';
                    $html .= '<p>';
                    $html .= '<a href="' . get_permalink() . '">';
                    $html .=  get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
                    $html .= '</a>';
                    $html .= '</p>';
                    $html .= '</section>';

                    $html .= '<aside class="bootstrap-title-info">';
                    $html .= '<p>';
                    $html .= '<a href="' . get_permalink() . '">'.get_the_title().'</a>';
                    $html .= '</p>';
                    $html .= '<p class="text-muted">' . get_the_date() . '||'. getPostViews(get_the_ID()) . '</p>';
                    $html .= '</aside>';
                    $html .= '</article>';
                    echo $html;
                endwhile;   
                ?>
                    </div> <!-- End row of popular posts -->
                </div> <!-- End tab-pane of popular posts -->   
        <?php } ?>

how can i add conditional statement to this code

Read More
$html .= '<a href="' . get_permalink() . '">';
                    $html .=  get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
                    $html .= '</a>';

Note: i want to say if has post thumbnail put the posts thumbnail and if not has the thumbnail put the first image instead of the post thumbnail

Related posts

Leave a Reply

2 comments

  1. $thumb = get_the_post_thumbnail(get_the_ID());
    if(!empty($thumb))
    $image = $thumb;
    else {
    $image = '<img src="';
    $image .= catch_that_image();
    $image .= '" alt="" />'; }
    

    And put your function as

    function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=['"]([^'"]+)['"].*>/i', $post->post_content, $matches);
      $first_img = $matches [1] [0];
    
      if(empty($first_img)){ //Defines a default image
        $first_img = "/images/default.jpg";
      }
      return $first_img;
    }
    
  2. Something like this could work:

    $html .= '<a href="' . get_permalink() . '">';
    if ( has_post_thumbnail() ) {
         $html .=  get_the_post_thumbnail(get_the_ID(), array('class' => 'img-responsive '.$YPE_options['YPE_sidebar_PRC_thumb_style'].''));
    } else {
         $html .= "<img src='". echo wp_get_attachment_image_src(0,'thumbnail') .'" class="img-responsive ' .$YPE_options['YPE_sidebar_PRC_thumb_style'].' " />';
    };
    html .= '</a>';