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

时间:2014-08-06 14:26:51

标签: php wordpress

我网站上的新闻文章包含一个附有标题的精选图片。

我的问题是,当文章不包含精选图片时,标题仍会出现,并且其中包含新闻文章的摘录。

<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。

1 个答案:

答案 0 :(得分:0)

您需要使用'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

相关问题