如何在查询中仅显示特定数量的帖子?
问题描述:
I am trying to query for the 3 most recent posts in the home page of my website powered by Wordpress:
wp_reset_postdata();
wp_reset_query();
query_posts('showposts=3');
rewind_posts();
while (have_posts()) : the_post();
// do stuff.
the_title();
endwhile;
However, 7 posts are being displayed. Furthermore, not all of these 7 posts are the latest posts. Above this query, there are other queries.
As you see, I have tried reseting the post data and the query data to no avail. What other factors could affect the posts retrieved?
我正在尝试查询我的网站主页上由Wordpress提供的3篇最新帖子: p>
wp_reset_postdata();
wp_reset_query();
query_posts('showposts = 3');
rewind_posts();
while(have_posts()):the_post( );
//做东西。
the_title();
endwhile;
code> pre>
但是,正在显示7个帖子。 此外,并非所有这7个帖子都是最新帖子。 在此查询之上,还有其他查询。 p>
如您所见,我尝试重置帖子数据和查询数据无济于事。 还有哪些因素会影响检索到的帖子? p>
div>
答
Be sure that you don't put that code in another have_posts
loop.
This should work:
query_posts( array( 'posts_per_page' => 3, 'orderby' => 'date', 'order' => 'DESC' ) );
while (have_posts()) : the_post();
// do stuff.
the_title();
endwhile;