0

我想在主页上使用多个循环。首先我想显示特定类别的帖子,然后是所有帖子,包括我上面包含的类别。但是当我使用第二个循环而不使用 query_posts 时,前一个循环的帖子被排除在外。

例如:

<div class="special_category" >
    <?php query_posts('category_name=special_cat&posts_per_page=10'); ?>
        <?php while (have_posts()) : the_post(); ?>
            <!-- will get special_cat posts -->
        <?php endwhile;?>
</div>

<div class="latest_posts">
        <!-- as i want do display all posts, so I don't use query_posts. -->
        <?php while (have_posts()) : the_post(); ?>
            <!-- this will exclude the posts of above special_cat -->
        <?php endwhile;?>
</div>

如果我在第二个循环中使用 query_string(即使没有传递任何参数),那么它包括帖子。

<div class="latest_posts">
        <!-- i used query_posts without any arguments -->
        <?php query_posts(''); ?>
        <?php while (have_posts()) : the_post(); ?>
            <!-- now this will get all posts -->
        <?php endwhile;?>
</div>

所以我的问题是,它是否意味着像那样工作,即。排除上述循环的帖子,还是我做错了什么?为什么不使用 query_posts 就无法获取所有帖子?谢谢。

4

2 回答 2

1

第一个查询将影响第二个循环,直到您重置它

<?php wp_reset_query(); ?> 在第一个循环后添加

更多信息在这里http://codex.wordpress.org/Function_Reference/wp_reset_query

于 2012-04-23T11:25:31.913 回答
0

如果您使用多个帖子循环,您应该使用wp_query
这样您也不必重置查询。

于 2012-04-23T12:42:31.623 回答