如果我的WordPress帖子不包含特色图片,标题仍然出现,但它包含帖子摘录


If my WordPress post does not contain a featured image, the caption still appears but it contains the post excerpt

我的网站上的新闻文章包含一个特色图片,并附有标题。

我的问题是,当一篇文章不包含特色图像时,标题仍然出现,并且它包含新闻文章的摘录。

<div class="single-img-container">
  <?php 
    the_post_thumbnail('full', array('class'=>"img-responsive single-news-img"));
    echo '<div class="news-img-caption">'.get_post(get_post_thumbnail_id())->post_excerpt.'</div>'; 
  ?>
</div>

我的理想结果是,如果不包含特色图像,标题div将不会出现。

您需要使用'has_post_thumbnail()'条件来确保帖子/页面具有特色图像。下面的代码将输出标记'…,

<?php if ( has_post_thumbnail() ) : ?>
<div class="single-img-container">
  <?php 
    the_post_thumbnail('full', array('class'=>"img-responsive single-news-img"));
    echo '<div class="news-img-caption">'.get_post(get_post_thumbnail_id())->post_excerpt.'</div>'; 
  ?>
</div>
<?php endif; ?>

参考:http://codex.wordpress.org/Function_Reference/has_post_thumbnail