get_template_part函数并在Wordpress上添加缩略图

时间:2013-11-27 21:31:10

标签: php wordpress

在任何页面中添加此功能:

get_template_part('recent', 'posts');

if(have_posts()){
while(have_posts()){ the_post(); ?>
    <div class="site-content clearfix"><?php the_content(); ?></div>
<?php }
}

它正在工作,在我的页面上显示5篇博文。但它只有标题和发布日期。我想在这篇文章中添加缩略图。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

你可以在The Loop中添加这样的东西:

if( has_post_thumbnail() ) {
    the_post_thumbnail();
}

您的帖子需要设置精选图片(以前称为后缩略图)。

<强>被修改

更深入的代码:

if( have_posts() ){
    while( have_posts() ){ 
        the_post(); 
        if( has_post_thumbnail() ) {
            the_post_thumbnail();
        }
        ?>
        <div class="site-content clearfix"><?php the_content(); ?></div>
        <?php 
    }
}

但请注意,如果帖子没有特色图片集,则the_post_thumbnail()将不会被调用。

参考

相关问题