Php 在WordPress主页上显示最新的2篇贴子

Php 在WordPress主页上显示最新的2篇贴子,php,wordpress,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 =

我正在剪切我的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\u posts
替换为
newwp\u query
,但在这种情况下,它会显示所有粘性帖子,而不是仅显示2篇


有没有建议如何调整上述代码并使其正常工作?

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


是的,这正是我需要的。谢谢,真是太好了!没问题:)