Multiple Loops without Duplication

If you need to break display your posts in different sections (like in Magazine themes), where you want to display a fixed number of posts first, and the remaining later, you can use this code to make multiple loops (with offsetting the fixed posts in the later loop).

The first loop that will pull and display 5 recent posts :

<?php
query_posts('showposts=5');
$ids = array();
while (have_posts()) : the_post();
$ids[] = get_the_ID();
the_title();
the_content();
endwhile;
?>

And, the second loop that will exclude the above posts and show the remaining ones :

<?php
query_posts(array('post__not_in' => $ids));
while (have_posts()) : the_post();
the_title();
the_content();
endwhile;
?>

This code basically pulls the post IDs that are not contained in the array $ids[].

Revisions

There are no revisions for this post.

Tags: , ,

No comments yet.

Leave a Reply