在WordPress主页上显示最新的2个粘贴帖子

时间:2012-09-09 12:07:33

标签: php wordpress

我正在削减我的WordPress主题,我想在我的Wordpress主页顶部添加最新的2个粘贴帖子。为此,我使用以下代码:

<div class="trending-right">
   <?php
     $sticky = get_option( 'sticky_posts' ); // Get all sticky posts
     rsort( $sticky ); // Sort the stickies, latest first
     $sticky = array_slice( $sticky, 0, 2 ); // Number of stickies to show
     query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) ); // The query

     if (have_posts() ) { while ( have_posts() ) : the_post(); ?>
     <div class="trend-post">
     <div class="thumb"><?php the_post_thumbnail(array(150,100)); ?></div>
     <div class="title"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></div>
     </div>
     <?php endwhile;?>
     <?php } else { echo ""; }?>

</div>

现在代码工作正常并显示最新的2个粘贴帖子,但它也会从主页中删除所有其他列出的帖子,并仅显示那2个粘贴帖子。我尝试将query_posts替换为new WP_Query,但在这种情况下,它会显示所有粘贴帖子,而不是仅显示2个。

有关如何调整上述代码并使其有效的任何建议吗?

1 个答案:

答案 0 :(得分:3)

查看您的代码,我假设您刚刚向我们展示了粘性循环,并且还有另一个查询来显示模板中其他位置的其他帖子?你应该使用wp_reset_query();在您的自定义查询之后,这是Codex中的条目;

Wordpress - resetting custom query