user966582
user966582

Reputation: 3313

WordPress: Using multiple post loops?

I want to use multiple loops on the homepage. First i want to display the posts of a specific category, and then all posts including the category which I included above. But when I use the second loop without using the query_posts, the posts of previous loop are excluded.

For example:

<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>

If I use the query_string (even without passing any arguments) in the second loop, then it includes the posts.

<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>

So my question is that, is it meant to work like that, ie. exclude the posts of the above loop, or am I doing something wrong? Why it wont get all posts without using the query_posts? Thanks.

Upvotes: 0

Views: 1070

Answers (2)

janw
janw

Reputation: 6662

If you use multiple posts loops you should use wp_query.
That way you also don't have to reset the query.

Upvotes: 0

Sujith Kumar KS
Sujith Kumar KS

Reputation: 1043

The first query will affect the second loop until you reset it

Add <?php wp_reset_query(); ?> after first loop

More info here http://codex.wordpress.org/Function_Reference/wp_reset_query

Upvotes: 1

Related Questions