简体   繁体   English

如何在查询中仅显示特定数量的帖子?

[英]How can I show only a certain number of posts in a query?

I am trying to query for the 3 most recent posts in the home page of my website powered by Wordpress: 我正在尝试查询由Wordpress支持的我的网站主页中的3篇最新帖子:

    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. 但是,正在显示7个帖子。 Furthermore, not all of these 7 posts are the latest posts. 此外,并非所有这7个帖子都是最新帖子。 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? 还有哪些因素会影响检索的帖子?

Be sure that you don't put that code in another have_posts loop. 请确保不要将该代码放在另一个have_posts循环中。 This should work: 这应该工作:

query_posts( array( 'posts_per_page' => 3, 'orderby' => 'date', 'order' => 'DESC' ) );
    while (have_posts()) : the_post();
        // do stuff.
        the_title();
    endwhile;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM