Multiple loops on the same page in WordPress 1.5

12 April 2005

I’ve been meaning to write up some of my WordPress-fu since I started playing with it. This is partly because the WordPress support forums are staggeringly unhelpful at times, especially to medium-skill users. (Note: this is not a flame or a troll; it is an issue I will return to at a later date. Whilst I like the product, I have several worries and issues with the community around it).

This is about the first big chunk of fiddling I did with WordPress, and makes it very useful as a more general purpose CMS, as well as if you have many categories on your site and want to display them all, at once, differently. So, without further ado: how to display multiple loops in WordPress 1.5.

“The Loop”, in the parlance of WordPress, is your main piece of syntax for display posts. The Loop basically says “if there are posts, display each post in the following manner“. In 1.5, they made WordPress behave more in the manner of an if/while…do statement, which is actually a much clearer way of understanding it than the old method.

Now, not everyone wants to display all their posts at once – some are in inappropriate categories. It’s very easy to display posts from a single category with an if statement. But what if you want to have multiple loops on one index page – say, for instance, if you have a magazine in sections and want to show the latest post in each category on your front page?

It’s not so hard. The trick to having multiple loops is to realise that you need to re-set the query string that each loop runs, for each loop on the page. Best explained with some code:

<?php query_posts('category_name=CatName&showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
<i>Your display code goes here</i>
<?php endwhile; ?>

Essentially, you force the query string to show you 1 post from category CatName. Then you put in a loop as shown above (note no if statement). It’ll display that one, latest post however you have formatted your display code.

What’s nifty is if you then repeat this chunk of code, but say, with a different CatName, you’ll get the top post in that category appearing when the loop runs – because you’re now running a different query. You’re not limited to 1 at a time, either; you can use it to display any number of posts. There’s a lot you can do, as I’ll show in a few more pieces like this, by manually overriding the query string to suit your needs.

As you can see, WordPress stops being a nice little weblog engine and suddenly becomes a very useful general purpose CMS; you can write index pages with more than one loop, and categorise your content very effectively (of course, this works best with posts that are only in one category).

I hope that’s been of use. Next up in this series : how to code “more like this” statements to show you recent posts in a category and recent posts by an author.

18 comments on this entry to date.

  • 12 Apr 2005
    If Else said... 1

    Good post. It’s quite impressive how much functionality actually lies beneath the hood and tutorials such as the one that you’ve provided are a benefit for the community.

    Nice job!

  • 12 Apr 2005
    If Else said... 2

    Hmmm… could I ask a favour? My email is being shown publically on your site. Would it be possible for you to either remove the email address or change your design to not display it?

    Spam is enough of an issue as it is:-) Thanks.

  • 19 May 2005
    Dan said... 3

    Thanks for this; I hadn’t realised that it was so easy(-ish) to mess around with and configure.

  • 5 Jun 2005
    Lara said... 4

    Thanks much, but I’ve run into a problem.

    I’m doing a magazine design just like you said, but I want the reader to click on “more” and be brought to a regular reading page with just that post and a few others from that specific category. How can I do this? Thanks!

  • 20 Jun 2005
    Grace said... 5

    YOU = THE BEST! This is just what I was looking for.

  • 29 Jun 2005
    Brendan said... 6

    YES. YES YES YES. THANK YOU.

  • 29 Jun 2005
    Christopher said... 7

    Thanks so much for this. The entry on the Loop in the Codex just didn’t click and this did.

    Thanks again.

  • 3 Jul 2005
    Jeff Werner said... 8

    Another thanks, and just in time for a WordPress (and PHP) newbie.

    I too am creating a magazine-style site, where I want to seperate the latest three entries from three seperate categories on the home page. Looks like your method will work, whereas the other Loop methods I read about could only be used once per page.

  • 6 Aug 2005
    pamG said... 9

    Thank you so much for this clarification. I have one lingering question, as the Codex offers zip on the issue: is there a point where the number of loops, combined with the number of posts, would sort of max out, and start causing MySQL “out of memory” error messages? By way of example, I came to WP from b2evolution. I converted those blogs to parent cats. I’ve already done the multiple loop deal on some parent-cat pages, which have only a few child-cats and relatively small # of posts. But I’m too paranoid to even attempt the same with a larger parent-cat. It has about 15 child-cats, and that combo page would have 400-500 posts (titles, dates only — like an index). I backed up the DB, but as a newbie, frankly, I’m too fearful to try it — without at least some confirmation that I’m not begging for trouble.

    Again, many thanks.

  • 29 Sep 2005
    Scott said... 10

    Ahhh… thank you so much.

  • 10 Nov 2005
    Ed said... 11

    I am looking to list my comments in the sidebar with the post in the main part of the page. Can this be done?

  • 13 Dec 2005
    Non Flame said... 12

    This is great code (if you explained the ‘display code code here’ bit)

    But the support you’re providing seems lacking, not an attack or anything it’s just that people asked for help a long time ago. I need help with this, but know I won’t receive it. That being said, you’ve brought me closer to understanding how to do this more then any other site (and I’ve been looking for weeks).

    So thanks :)

  • 13 Dec 2005
    Tom said... 13

    Sure, I can explain the “display code goes here” – all that is is the code to render the post (eg the_content(), the_title(). The usual stuff you’d write to display a post, only this time it’s displaying a limited number of posts, from a single category, or whatever.

    You are correct in noting that people asked for further help a long time ago. I would also point out that I’m in no obligation to provide it.

    I might possibly have a quick run through this list tonight and try and answer some questions – or at least email the people directly. I can’t email you, Non Flame, because you didn’t give me a valid email address – but never mind, I’m responding here. Some of the queries probably do deserve a response as they’re directly related to my post and fairly technical. “Listing comments in the sidebar” is pretty much entirely unrelated to this post.

    Finally, I think the tips I’ve provided here – which I worked out myself, remember – are enough to let other people explore further for themselves.

    If you’ve got more direct, on-topic questions, I’d be happy to try to help, Non Flame, but I’m ludicrously busy at work right now. Still, it’ll go in my inbox, and I’ll get back to you when I can.

  • 10 Jan 2006
    Clint M Chilcott said... 14

    “It’s very easy to display posts from a single category with an if statement.”

    Could you help me figure out how to do this? I hate to be a bother but I have been up all night wrestling with PHP and searching for a simple solution.

    On my wordpress blog I have a category dedicated to my podcast. I want to make a page that displays some info on the podcast and how to subscribe. Then below that it displays every post in the “Podcast” category.

    I thought it should be simple but I am in a world of hurt. Can you help? Thanks!!

    -Clint M Chilcott

  • 12 May 2006
    Griff said... 15

    Thank you very much for your article on multiple loops. I’ve had much more success with yours then any other (including wordpress codex/forums).

    You should consider a Pay Pal donate button. ;)

  • 13 Jul 2006
    flys said... 16

    hi, i’m not able to use this code, can someone help me ?….

    I should replace just this:

    with this?:

    because the rest of the code seem to be the same …
    thanks in advance

  • 13 Jul 2006
    flys said... 17

    hi, i’m not able to use this code, can someone help me ?….

    I should replace just this:

    with this?:

    because the rest of the code seem to be the same …
    thanks in advance

  • 13 Jul 2006
    flys said... 18

    hi, i’m not able to use this code, can someone help me ?….

    I should replace just this:

    if (have_posts()) :

    with this?:

    query_posts(‘category_name=CatName&showposts=1′);

    because the rest of the code seem to be the same …
    thanks in advance

Similar posts to this one

My links and notes for this day