Triple Loop for WordPress
Two of the themes developed by Perishable Press, Apathy and Information, depend on three WordPress loops to operate as intended. For each of these themes, the three loops consist of two “side” loops and one main loop. The side loops each display posts from one specific category, while the main loop displays posts from every category not specified in the side loops.
WP_Query
and other “looping” techniques to customize the WordPress loop, create multiple loops, and so forth. You can learn more about these techniques in my tutorial, 4 Ways to Loop with WordPress. The basic principles of this article still apply, but you should be using WP_Query
instead of query_posts
whenever possible.Intro
There are many different multi-loop configurations currently available for WordPress users. Needless to say, despite a wide variety of available loop setups, implementing a customized multiple loop frequently requires a great deal of time of energy. Certain loop sets accomplish one task, but fail at another, while others refuse to provide enough flexibility in general. Indeed, after countless rounds of trial and error establishing multiple loops, we finally developed the almost-perfect triple-loop configuration.
The Upside
The Perishable Press triple loop for WordPress provides several specifically designed features:
- Each side loop displays posts from any unique category.
- Each side loop displays any unique number of posts.
- The main loop may exclude posts from any category.
- The main loop displays no posts included in either side loop.
- The main loop displays any unique number of posts.
- Post views function properly for any post in any loop.
The Downside
The Perishable Press triple loop for WordPress currently encompasses these specific caveats:
- Archive navigation is only possible for the main loop.
- The main loop must precede both of the side loops.
- The “side loops” technically are neither “side” nor “loops”.
Additionally, the main loop will look at the first “x” number of posts, and display only those posts which are not included in either side-loop category. Thus, if the most recent “x” number of posts all belong to either of the side categories, there will be no posts to display in the main loop. Fortunately, there are several relatively simple solutions for this situation, which will be discussed at the end of this article.
The Main Loop
As mentioned, the main loop must appear before the two side loops. Fortunately, the magic of CSS nullifies the restriction imposed by this requirement. Another important point to mention involves the lack of archive navigation (e.g., << previous | next >>
) for either of the side posts. Although this may be possible, I have yet to determine an optimal method.
Nonetheless, complete navigation comes standard with the main loop. The side loops work perfectly for displaying the latest “x” number of posts from their respective categories. An elegant solution would be to provide a nice “Read more posts from this category »” link for each side loop display.
Moving along, our first task is to exclude our two chosen side categories from the main loop. Here, we are instructing WordPress to process posts as usual, with the exception that all posts from either side-loop category are ignored. Here are the first three lines of the main loop:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('7') && is_home() ) continue; ?>
<?php if ( in_category('8') && is_home() ) continue; ?>
Above, the loop starts with the usual if():while()
statement, and then proceeds into a key pair of conditional if
statements. The first conditional statement filters out all posts from category 7, while the second does the same for category 8. The remainder of the main loop is expressed as usual, as generalized here, replete with several key elements:
// the first loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('7') && is_home() ) continue; ?>
<?php if ( in_category('8') && is_home() ) continue; ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php comments_template(); ?>
<?php endwhile; ?>
<?php posts_nav_link('','','« Previous') ?><?php previous_post('« %', '', 'yes'); ?>
<?php posts_nav_link('','Next »','') ?><?php next_post('% »', '', 'yes'); ?>
<?php else : ?>
<p>Sorry..</p>
<?php endif; ?>
The previous loop example includes title, contents, comments, and navigational elements, as well as an else
statement issuing a nice “Sorry” message for those awkward, missing-post moments. For more information regarding the WordPress loop, check out the WordPress Codex and 4 Ways to Loop with WordPress.
The Side Loops
As previously noted, the term “side loops” is a bit of a misnomer. Instead of the standard if():while()
loop intro, the two secondary loops each begin with get_posts()
and foreach():start_wp()
statements. With such, the secondary loops function similar to standard loops, but employ a different functional method. As far as calling them “side” loops, well that is just a convenient name, as they may display posts anywhere, not just on the side of a web page.
Now, with the main loop in place, it is time to add the two side loops. Here is the basic setup:
// the second loop
<?php query_posts('cat=7&showposts=3'); ?>
<?php $posts = get_posts('category=7&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
// the third loop
<?php query_posts('cat=8&showposts=3'); ?>
<?php $posts = get_posts('category=8&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
Okay, in the previous example, the second loop is querying all posts in category 7 and displaying the first three, beginning with the most recent posts. The second post acts similarly, with the exception that it queries posts from category 8. Each loop also contains a title and excerpt element, included for the sake of demonstrative clarity.
Several important points should be mentioned here. For each of the two side loops, there are two parameters indicating category number, two parameters indicating the number of displayed posts, and one parameter indicating an offset value. For this triple-loop configuration to function properly, it is essential to provide matching parameters for both category and post display number. That is, the values cat=8&showposts=3
must correspond precisely to the values category=8&numberposts=3
. Same category, same number of posts to display. Meanwhile, the offset
parameter should remain at zero.
Thus, to customize the two secondary loops, simply choose two unique category ID’s and corresponding values specifying how many posts from either category you wish to display.
All Together Now
Now that I have bored you nearly to death with all of the gruesome details, here is the complete code for the Perishable Press triple loop for WordPress:
// the first loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('7') && is_home() ) continue; ?>
<?php if ( in_category('8') && is_home() ) continue; ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php comments_template(); ?>
<?php endwhile; ?>
<?php posts_nav_link('','','« Previous') ?><?php previous_post('« %', '', 'yes'); ?>
<?php posts_nav_link('','Next »','') ?><?php next_post('% »', '', 'yes'); ?>
<?php else : ?>
<p>Sorry..</p>
<?php endif; ?>
// the second loop
<?php query_posts('cat=7&showposts=3'); ?>
<?php $posts = get_posts('category=7&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
// the third loop
<?php query_posts('cat=8&showposts=3'); ?>
<?php $posts = get_posts('category=8&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
That’s all there is to it — copy, paste, and tweak to taste!
Caveat
As previously discussed, the main loop will look at the first “x” number of posts, and display only those posts which are not included in either side-loop category. Thus, if the most recent “x” number of posts all belong to either of the side categories, there will be no posts to display in the main loop. Fortunately, there are several relatively simple solutions for this situation:
- Increase the number of posts that are displayed in the main loop via the Admin > Options > Reading panel.
- Decrease the number of posts that are displayed in the side loops via step 5a and 5b in the Installation instructions above.
- Simply add a few more (or however many it takes) posts to categories appearing in the main loop (i.e., non-side-loop categories).
A Million Loops
With a little imagination, it is easy to envision a case where more than three loops might be required. Fortunately, our method for dishing three WordPress loops may be extrapolated into four WordPress loops, five WordPress, loops, six WordPress loops, …even a million WordPress loops. Well, okay, maybe not. Nonetheless, for any reasonable number of loops, if you have the categories, we have the code!
Here is a hypothetical, generalized example demonstrating “n” number of WordPress loops:
// the first loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('2') && is_home() ) continue; ?>
<?php if ( in_category('3') && is_home() ) continue; ?>
<?php if ( in_category('4') && is_home() ) continue; ?>
<?php if ( in_category('5') && is_home() ) continue; ?>
. . . . .
. . . . .
. . . . .
<?php if ( in_category('n') && is_home() ) continue; ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
// the second loop
<?php query_posts('cat=2&showposts=3'); ?>
<?php $posts = get_posts('category=2&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
// the third loop
<?php query_posts('cat=3&showposts=3'); ?>
<?php $posts = get_posts('category=3&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
// the fourth loop
<?php query_posts('cat=4&showposts=3'); ?>
<?php $posts = get_posts('category=4&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
// the fifth loop
<?php query_posts('cat=5&showposts=3'); ?>
<?php $posts = get_posts('category=5&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
. . .
. . .
. . .
// the nth loop
<?php query_posts('cat=n&showposts=3'); ?>
<?php $posts = get_posts('category=n&numberposts=3&offset=0');
foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endforeach; ?>
Well, surely I have worn out my welcome here. Perhaps you will be so kind as to drop a comment to share your particular experience with multiple WordPress loops. Cheers!
50 responses to “Triple Loop for WordPress”
Thanks Perishable —
That did the trick! What an amazing site you have here. The knowledge base is remarkable.
Glad it worked for you! Thanks for the feedback and kind remarks ;)
yeah, that’s what I am talking about! This is the best tutorial on multiple loops I’ve read and it works like a charm, even when the first loop is a page (not a category).
KUDOS!
I wish it had been easier to find – I can’t tell you how many plugins and wp “tutorials” I tried before this clear, concise (and more importantly) ACCURATE and FUNCTIONING tutorial on using multiple loops!
THANK YOU!
My pleasure, Casey — thank you for the rave review! :)
Just what I needed to get me out of the loop!
Thanks for the effort of putting this together. It’s what I’ve been looking for. Gonna print it out and test things out myself.
I wanted to do much more than just listing 10 recent posts. Things like popular posts, featured posts, related posts and all …
Absolutely! Triple loops are perfect for creating unique and versatile blog layouts via WordPress. Don’t have too much fun! ;)
Hi Perishable,
Firstly, thank you for this tutorial. I am a relative novice and this was excellent. One thing, I have the drop down category menu widget going and there seems to be a conflict. My home page now comes up as “Links | Philatelic Database” instead of just “Philatelic Database”. I can live with this yet I just thought i’d let you know in case you know of a fix.
Thanks again,
Richard
Hi Richard, thanks for the positive feedback. As for the issue with the title of your home page, there are a number of things that could be causing the conflict. First, does the term “Links” disappear when the triple loop is removed? And/or, does the term disappear from the title (as displayed at the top of the browser) when the category widget is removed? Checking either of these items should provide additional clues.. If you need more help with this, please contact me directly. Thanks!
Trying to get it to work with a date query and not having any luck.
Ie:
loop 1 = mondays posts
loop 2 = tuesday posts
and so on…
You may want to try setting a variable for the
get_the_time()
tag, and then employ a series ofif()/else()
conditions within a multiple-loop configuration.okay thanks I will give it a shot
actually I found something simpler but you led me to investigate it.
if you use the_date inside the loop it only displays the date once, so even if you have 3 posts from that day you can make the date a header.
different then the_time, pretty sweet though!