Perishable Press Triple Loop for WordPress
Published Wednesday, November 22, 2006 @ 7:12 pm • 52 Responses
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.
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, visit the WordPress Codex.
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!
About this article
Related articles
- Multiple Loops and Multiple Columns with WordPress, (X)HTML and CSS
- Super Loop: Exclude Specific Categories and Display any Number of Posts
- Easily Adaptable WordPress Loop Templates
- Horizontally Sequenced Display Order for WordPress Posts in Two Columns
- 6 Ways to Customize WordPress Post Order
- Perishable Press WordPress Plugins
- WordPress Notes Plus
Dialogue
52 Responses Jump to comment form
January 16, 2007 at 6:29 pm
i just put two loops in my template on the index.php file and for some reason now the and no longer works properly in the sidebar. it works on the single page, but on the home page the sidebar just doesn’t show up at all - i’m trying to just have certain elements of the sidebar show up on the home page and then different stuff show up on the single post page.
this is definitely related to the second loop because when i take it out, everything works fine.
any idea why?
February 20, 2007 at 11:27 am
my question is that I really want one of my “side loops” to be at the top of my page just under the header.
I would like to use it as a feature post, but if the code for the second loop has to follow the main loop how can I get it to come in at the top of my page.
May 1, 2007 at 9:10 am
I am trying out the apathy template but I am getting the error in the right hand column
Fatal error: Call to undefined function: c2c_get_recent_posts() in C:\websites\217\ypsinews.com\wp-content\themes\apathy\index.php on line 155
www.ypsinews.com/index.php
This is running on IIS6
If you have any suggestions they would be most appreciated
Cheers!
_ Steve
May 1, 2007 at 9:14 am
Whoops, I didn’t enable the two plugins, sorry about that. - Steve
September 18, 2007 at 8:23 am
is it possible to use this multiple loop setup to display the main loop on one page, and one of the side loops on a different page… a WP Page?
I’m looking for a way to split posts into two streams, so to speak, and display on separate pages within the same blog.
Just wondering if this is the right direction at all…
October 7, 2007 at 9:51 am
Great tutorial dude you really saved me from google`ing arround all day searching for multiple loop examples
October 16, 2007 at 7:27 pm
My set up, still in local-test-run, is: sidebar1.php then index.php then sidebar.php.
i want to have the sideloops in sidebar1.php and the post content in index.php.
So far, I have got the sideloops working but the post content in index.php which shouldbe showing all other posts instead shows the “Sorry” message no matter how many posts I have in the other categories (not the sideloop cats). Any ideas?
p.s. thanks for taking the time to publish your tips.
October 17, 2007 at 9:14 am
I somewhat fixed the problem after reviewing the procedure. I think the problem was that index.php was calling for sidebar1.php before the content. Once I moved the call everything fell into place, except the main content is now in the lefthand sidebar. I can live with that with a few design tweeks.
Thanks again.
November 6, 2007 at 6:15 pm
I’m trying to implement the hiding of posts in the main section if they show up on a sidebar via your “if ( in_category(”)” statements and everything does display correctly per category.
However, the next and previous links don’t work as expected. The home page shows posts in the main section, but if you click on “previous” the resulting page doesn’t show any posts in the main area, although the second and third loops do work showing their posts.
If I comment out the lines in the index file to hide posts in the categories showing posts in other loops, all is good. This is on a server on my workstation so I can’t show it to you live as yet. Any thoughts?
Thanks in advance.
November 7, 2007 at 10:32 am
>>[T]here may be no additional posts to show on the next archive page. The solution, of course, is to populate the database with more posts[.]<<
Absolutely correct! Thanks so much for the timely response. All is working fine now.
What a great resource, this site!!!
November 14, 2007 at 9:38 pm
I am so bewildered.
I have an old blog (running WP 2.0.5) and I can get this to work fine. Actually, I only want the code from the side loops for a project on a particular page. My tests there work fine.
But on the new blog (running WP 2.1.2) where I actually want the code to work, I cannot get it to work to save my life.
I feel it will be something silly.
I have the EXEC-PHP (v4.0) plugin installed in both blogs. PHP seems to be working ok; I get the expected result when I try the test…
<?php echo "This is the Exec-PHP 'Hello World'"; ?>
…so no problems there.
The option “WordPress should correct invalidly nested XHTML automatically” is not checked.
I am not using the WYSIWYG editor. The ‘unfiltered html’ and ‘exec php’ capabilities are new to me, but I don’t believe this to be a problem since I’m posting from the admin account and since the Exec-php test is working correctly.
But I just cannot get it to work. Here is the code that works in the old (v2.0.5) blog but not in the new (v2.1.2) blog.
<div style="background-color:#CCCCCC; float:left; width:46%; padding: 10px">
<?php query_posts('cat=7&showposts=2'); ?>
<?php $posts = get_posts('category=7&numberposts=2&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endforeach; ?>
</div>
The category is populated with posts.
The code stands alone on a WP page (for now). When I save the page and try to load it, it usually churns for a while and times out. I get one line of the background color defined in the DIV statement.
I really hope someone can save my sanity and help me understand why it works one place and not the other.
Thank you very much.
November 15, 2007 at 10:00 am
Danny,
I compared your code to mine (I use WP2.3) and it looks the same. One thing that tripped me up, and was very silly, was the actual category number. I had been working on my localhost which used a different number as the actual online blog (I hadn’t made a fresh copy of the DB and installed it locally). I’m a novice and DID get this Triple Loop working. Forgive me if my suggestion is BEYOND obvious.
: )
November 16, 2007 at 10:21 am
Lauren,
No suggestion is too obvious including making sure that I have the monitor on.
I did get tripped up early with the category #, and especially making sure that both parameters (category & number of posts) were the same both places, but I am quite sure that it is correct.
I can’t figure what the problem is, and it’s quite vexing. I would be glad for any help or other suggestions. (The problem is outlined in comment #20.)
Danny
November 18, 2007 at 1:43 pm
Hi Jeff,
Not sure exactly what you are asking…
Both blogs have EXEC-PHP installed. The old blog doesn’t really need/use the loops, but I tested the code there first, and it worked fine.
The new blog is where I would like to use the loops. EXEC-PHP is installed there also, and as I said, this php test code worked fine on the new blog:
<?php echo "This is the Exec-PHP 'Hello World'"; ?>
So at least some php is executing okay on the new blog.
Thanks. I hope I answered what you were asking.
Danny
November 18, 2007 at 2:23 pm
That’s very interesting.
In my old blog (WP 2.0.5), I just created a WP page and slapped the php into the WP page and let EXEC-PHP handle it. Worked fine.
But definitely not working in the new blog (WP 2.1.2). Frankly, it didn’t occur to me put it in the code of the page itself especially since it worked it in the text of the WP page.
I’ll try it.
(I don’t have shell access and so am waiting for my host to help me restore to WP 2.1.2 since something went awry in an attempted upgrade to 2.3. But can hardly wait to try it.)
November 18, 2007 at 9:54 pm
Early signs for me are that it works as expected when put into the code of a page as you suggested (and intended). I was trying to get it to do something that it was not intended to do, and I so very much appreciate you helping me get straightened out.
This could be really sweet… thank you so very much.
November 21, 2007 at 1:50 am
I have the multiple loops working perfectly on a test blog on my workstation server, with no repeating of posts in the main blog area, however I’ve just taken the whole thing live and the posts in select categories do show up in the highlighted sections as desired, but they are repeating in the content area of the blog. Any suggestions would be much appreciated.
http://www.article6blog.com/
Arthur
November 21, 2007 at 1:53 am
Doah!!! Never mind — forgot to change the category numbers in the main index template. Now it’s working fine.
December 14, 2007 at 9:56 am
Hi, just a question and please sorry for my english :P
What about excluding from the side loops just the post (and not the category) displayed in the main loop?
Obviously while still assigning a category for each side loop.
December 18, 2007 at 4:47 pm
This is a great tutorial. One question. How do I order the posts alphabetically by the title for the second loop? Thanks for your help on this.
December 19, 2007 at 2:17 pm
I have several loops working on the blog, article6blog.com. On one of them there is a box that features the title of the most recent post in a certain category, along with that post’s Optional Excerpt info, and then below that are the titles of the five most recent posts from the same category. As it now stands, the most recent post’s title gets repeated in that secondary list area. Is it possible to call for the list of (x) most recent posts but have the actual most recent one skipped?
Thanks in advance for any advice.
December 26, 2007 at 12:38 pm
Thanks Perishable –
That did the trick! What an amazing site you have here. The knowledge base is remarkable.
December 31, 2007 at 10:41 am
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!
February 18, 2008 at 12:55 am
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 …
March 8, 2008 at 11:21 am
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
March 17, 2008 at 1:26 am
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…
March 18, 2008 at 12:40 pm
okay thanks I will give it a shot
March 18, 2008 at 1:19 pm
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!
March 19, 2008 at 10:14 am
Nice work with this article. I have referred to it for a number of projects and find it to be quite useful. Thanks for sharing it with us!
Trackbacks / Pingbacks
[ Comments are closed for this post. ]
If you have additional information, contact me.
← Previous post • Next post →
« WordPress on Crack: Tips for Faster Post Publishing • Time Test »
1 • shaf
December 26, 2006 at 11:02 am
i am just looking at this layout. please excuse the snooping.