If you're new here, you may want to subscribe to my RSS feed or get convenient updates via Email . Thanks for visiting!
First, a definition of “The Loop” from the WordPress Codex:
“The Loop” is a term that refers to the main process of WordPress. You use The Loop in your template files to show posts to visitors. You could make templates without The Loop, but you’d only be able to display data from one post.
Why would I want more than one loop?
- To have a “Featured” post that is always above all other posts
- To show a post (or two, or three) of a specific category, or categories on the main page
- To break the blogging mold and make your theme unique
How do I do it?
This was for a project I’m involved with called Show in a Box. I wanted two loops, the first showing one post from a selected category, and the other looping through all posts while excluding the previously displayed post. I am using the Sandbox theme as a base and will be working with the index.php file specifically. This should work on any theme in theory, but please know that all themes are different, so your code may contain more, or less code than referenced here. As a matter of course, always backup your files before changing anything. Oh, and did I mention that you should backup, backup, backup!
Here goes…so in the original index.php file, you’ll find “The Loop”. This is the chunk of code that WP uses to “loop” through all the posts and display them.
<?php while ( have_posts() ) : the_post() ?> <?php endwhile ?>
Now, this is the entire code of the index.php file. You’ll see a bunch of code inside “The Loop”, all of that stuff tells WordPress what things to display inside each post. Basically, this small bit of code says “loop through each post and display all of this information inbetween until I find no more posts”.
<?php while ( have_posts() ) : the_post() ?>
<div id="post-<?php the_ID() ?>" class="<?php sandbox_post_class() ?>">
<h2 class="entry-title"><a href="<?php the_permalink() ?>" title="<?php printf(__('Permalink to %s', 'sandbox'), wp_specialchars(get_the_title(), 1)) ?>" rel="bookmark"><?php the_title() ?></a></h2>
<div class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO'); ?>"><?php unset($previousday); printf(__('%1$s – %2$s', 'sandbox'), the_date('', '', '', false), get_the_time()) ?></abbr></div>
<div class="entry-content">
<?php the_content(''.__('Read More <span class="meta-nav">»</span>', 'sandbox').''); ?>
<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'sandbox') . '&after=</div>') ?>
</div>
<div class="entry-meta">
<span class="author vcard"><?php printf(__('By %s', 'sandbox'), '<a class="url fn n" href="'.get_author_link(false, $authordata->ID, $authordata->user_nicename).'" title="' . sprintf(__('View all posts by %s', 'sandbox'), $authordata->display_name) . '">'.get_the_author().'</a>') ?></span>
<span class="meta-sep">|</span>
<span class="cat-links"><?php printf(__('Posted in %s', 'sandbox'), get_the_category_list(', ')) ?></span>
<span class="meta-sep">|</span>
<?php the_tags(__('<span class="tag-links">Tagged ', 'sandbox'), ", ", "</span>\n\t\t\t\t\t<span class=\"meta-sep\">|</span>\n") ?>
<?php edit_post_link(__('Edit', 'sandbox'), "\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t<span class=\"meta-sep\">|</span>\n"); ?>
<span class="comments-link"><?php comments_popup_link(__('Comments (0)', 'sandbox'), __('Comments (1)', 'sandbox'), __('Comments (%)', 'sandbox')) ?></span>
</div>
</div><!-- .post -->
<?php comments_template() ?>
<?php endwhile ?>
Now, in order to tell this loop to only show one post, we have to tell it what category to look in and how many posts from that category to display. So we change that loop code from this:
<?php while ( have_posts() ) : the_post() ?>
…to this…
<?php $my_query = new WP_Query('category_name=featured&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
All of the code inbetween goes here
<?php endwhile; ?>
You can see in the code above, I have specified the category “featured” and set it to show 1 post. This could be any category we choose, and further, we could display as many posts from that category as we want.
So, at this point we still only have one loop on the main page, and if we were to view our site now, we would see only one post from the featured category on the main page. But wait, we want to display the rest of the posts underneath this right? So, what do we do? Add another loop on the same page. Here’s the code for the second loop:
<?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
Some more inbetween code stuff here.
<?php endwhile; endif; ?>
So the post above is the standard WP Loop with some extra parameters. Those extra parameters say “don’t duplicate the post I already have shown in the first loop”, and “update the post cache as you loop through the posts.”
So, that’s it (in a nutshell). I found most of this information here http://codex.wordpress.org/The_Loop#Multiple_Loops and by following some of the links there. I also used some good ‘ole trial and error;) I hope this helps, and if it does, please take a moment to comment and let me know.
















14 Comments
awesome.
this is really important work here.
I know how we crave to show multiple kinds of videos on the front page that are displayed with different rules.
so so awesome.
jay dedman’s last blog post..VIDEO: Open_the_networks
Thanks Jay,
This is only the tip of the iceberg, stay tuned!
Very Good Adam,
I would have to mess with it to really get it, but I think you have really documented it well.
BTW – I got that file. Haven’t looked at it yet, but thanks so much!!
Milt
Thanks Milt,
I hope I haven’t missed anything;)
How would you do multiple loops for different types of content. So if you had a static page for your homepage, there is obviously a loop to pull in that static content, but how would you add a loop to that page template to pull in blog posts instead of page posts?
I actually meant to ask how to pull in blog posts IN ADDITION TO a page post all within one page template?
Hi Erik, and thanks for commenting, I’m not sure I understand what you’re asking.
Can you try and rephrase you’re question and let us know what you’d like to accomplish? Then we can work backwards from your goal and find the parts to give you a solution.
I explained a bit better in the wordpress forum and figured out what I needed (which was basically what your post provides) — http://wordpress.org/support/topic/165447?replies=4. Thanks!
Is this the same as having snippets of numerous post on the home page? They may be in different categories but I want the visitor to the home page to be able to read a small part of each post. Thanks
Marian Mccanlesss last blog post..Happy New Year!
Hi Marian,
The short answer to your question is no, this is completely different from what you are trying to achieve.
Now the long answer…
What you’re describing is the “the_excerpt” function in the WP world, as opposed to a function named “the_content”.
When you look at your main blog page, the code that is displaying your posts is displaying “the_content”. That’s why you are seeing the entire post.
Now, if the code used to display your posts used “the_excerpt” function instead, you would see short summaries of your posts.
You can force individual posts to use “the_excerpt” by inserting the “More” tag after the first couple of lines of your post. Also, some themes have this built in, some don’t.
You go here for some further information:
http://www.sidelinestream.com/blogging/wordpress-excerpts.php
…and here’s a cool plugin I found for you…
http://www.dailyblogtips.com/homepage-excerpts-wordpress-plugin/
Hope that helps. Feel free to use the contact tab if you need more in-depth assistance.
hi there,
great article! i was wandering though if (or how) it is possible to add pagination to the query posts?
thanks
BTW, maybe that helps http://www.maxblogpress.com/plugins/dppp/use/
hi,
it’s me … again. i discovered another problem. do you have any idea why my comments aren’t working?
have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<div class="post" id="post-">
lorem ipsum
<a href="">Read more
hope you know what to do.
thanks
hm … well … i wander if you have a chance to read the source code somewhere!? cause it just got eaten up by wp …
One Trackback
[...] Multiple Loops on your Main Page – Why and How | WordPress Modder I wanted two loops, the first showing one post from a selected category, and the other looping through all posts while excluding the previously displayed post. [...]