我怎么能得到第一个图像,如果帖子没有帖子缩略图在wordpress


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

我为主题中流行的、最近的和评论最多的文章创建了侧边栏小部件。我有一些不包含图像缩略图的帖子。这是我的小部件

中5个帖子的热门查询帖子
<?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 } ?>

如何在此代码中添加条件语句

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

注意:我想说如果有post缩略图放帖子缩略图,如果没有缩略图放第一个图像,而不是post缩略图

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

把你的函数设为

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;
}

可以这样做:

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