One of my readers, Avinash, asked how he can create two distinct WordPress loops on the same page. Avinash wants to get the most recent posts first, and later create a new loops getting the 5 next posts, excluding the most recent post.
One of my readers, Avinash, asked how he can create two distinct WordPress loops on the same page. Avinash wants to get the most recent posts first, and later create a new loops getting the 5 next posts, excluding the most recent post.
To achieve what Avinash wants to do, we have to use the query_posts() function with the showposts and offset parameters.
The first loop:
query_posts('showposts=1'); // First loop, we only get the most recent post
if (have_posts()) :
while (have_posts()) : the_post(); ?>
// WordPress loop
endwhile;
endif; ?>
The second loop:
query_posts('showposts=5&offset=1'); // Second loop, we get 5 posts excluding the most recent.
if (have_posts()) :
while (have_posts()) : the_post(); ?>
// WordPress loop
endwhile;
endif; ?>
A little explanation about query_posts() parameters we used here:
showposts=5 specifies that you only wants to get 5 posts.
offset=1 specifies that you don't want to get the 1st most recent post.
Leave a Comment