The Loop is the heart of WordPress. It displays all the posts, categories, tags and archives.
Now the question is “why do we need multiple loops?”
Basically you might want to do something with one group of posts and then do something different to another group of posts, yet still display them both on the same page. While this is somewhat cryptic here are some practical examples of loops that you might want to implement into your WordPress theme:
- List your featured posts. This can be done by listing posts in a particular category or tagged with a particular tag.
- Have a category “videos” that you might also want displayed on the home page along side your main articles.
- Display a carousel with your portfolio above your main content.
- Display your posts using multiple loops and multiple columns.
- Create an Asides area on your blog
As always, besides the benefits there are also limitations and drawbacks to multiple loops:
- You can only have navigation for the main loop
- If you reset the main loop to allow for the other loops to work correctly you will loose the archive navigation
- You’ll have to create your own custom query
We’ll start with the basic loop:
<?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> <!-- do stuff ... --> <?php endwhile; ?>
Now the key to using multiple loops is that $wp_query can only be called once. In order to get around this it is possible to re-use the query by calling rewind_posts() or by creating a new query object.
In order to loop through the same query a second time, call rewind_posts(). This will reset the loop counter and allow you to do another loop.
<?php rewind_posts(); ?> <?php while (have_posts()) : the_post(); ?> <!-- Do stuff... --> <?php endwhile; ?>
This isn’t particularly useful but if you ever need it, it’s there for you to use!
List posts in a “featured” category with query_posts()
Now we start building something useful.
// Get the last 3 posts in the featured category. <?php query_posts('category_name=featured&showposts=3'); ?> <?php while (have_posts()) : the_post(); ?> <!-- Do featured stuff... --> <?php endwhile;?> //We reset the loop <?php rewind_posts(); ?> //We build the normal loop that will list out blog posts <?php while (have_posts()) : the_post(); ?> <!-- Do stuff... --> <?php endwhile; ?>
There is a rather annoying problem with this code. In case we want to use pagination for the normal loop that displays the blog posts, when we click ‘previous’ page 2 will display the same posts as on the homepage. The reason behind this is the rewind_posts(); function. Since it resets the loop we’ll display the first posts every time!
Next let’s see how we can make use of the navigation.
Create a new query object.
If you need to keep the original query around, you can create a new query object. This will also allow us to make proper use of our navigation!
<?php $my_query = new WP_Query('category_name=featured&showposts=3'); ?> <?php while ($my_query->have_posts()) : $my_query->the_post(); ?> <!-- Do special_cat stuff... --> <?php endwhile; ?>
The query object my_query is used because you cannot use the global have_posts() and the_post() since they both use $wp_query. Instead, call into your new $my_query object.
Store your query for future use
Another version of using multiple Loops takes a different approach to getting around the inability to use have_posts() and the_post(). To solve this, you need to store the original query in a variable, then re-assign it when with the other Loop. This way, you can use all the standard functions that rely on all the globals.
This piece of code will work on php5 servers only.
// saving the query <?php $temp_query = clone $wp_query; ?> <!-- Do stuff... --> //listing out featured articles <?php query_posts('category_name=featured&showposts=3'); ?> <?php while (have_posts()) : the_post(); ?> <!-- Do special_cat stuff... --> <?php endwhile; ?> // restoring the query so it can be later used to display our posts <?php $wp_query = clone $temp_query; ?>
If you liked this and want to learn more loop tricks for WordPress here are some links for you:
- Top 10 WordPress hacks from June â€˜09
- Get tags specific to a particular category on your WordPress blog
- 10 Useful WordPress Loop Hacks