【问题标题】:WordPress: Using multiple post loops?WordPress:使用多个帖子循环?
【发布时间】:2012-04-23 11:22:21
【问题描述】:

我想在主页上使用多个循环。首先我想显示特定类别的帖子,然后是所有帖子,包括我上面包含的类别。 但是当我使用第二个循环而不使用 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 就无法获取所有帖子? 谢谢。

【问题讨论】:

    标签: php wordpress loops


    【解决方案1】:

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

    在第一个循环后添加&lt;?php wp_reset_query(); ?&gt;

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2021-04-25
        • 1970-01-01
        • 1970-01-01
        • 2021-04-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-23
        相关资源
        最近更新 更多