• I thought I’d offer up wisdom found the hard way, several useful WordPress tricks to make it behave a bit more like a CMS and a bit less like a straight-up blogging engine. The first I’ve offered up is how to run multiple loops on an index page, in this case, to list individual posts in different categories. I know when I was researching this the information I found wasn’t too up-to-date (on the support forums) and thought I step-by-step guide might be appreciated by the next poor soul searching this fora in vain…

    anyhow, here you are:

    Multiple loops on the same page in WordPress 1.5

Viewing 15 replies - 1 through 15 (of 22 total)
  • Excellent. There’s a similar resource available on the Codex, but more resources is always good.

    Of particular interest to developers is the post on the query_post parameters.

    The entry in the codex for query_post is currently empty and I’d thought it’d be useful to write up a quick post on the arguments that query_post uses as well as what you can do with them.

    When I get a bit of time, I’ll transfer this info to the codex but in the mean time, enjoy:-)

    (BTW, If anyone else is willing to put this info into the codex, then by all means, go ahead.)

    Thread Starter infovore

    (@infovore)

    Thanks, ifelse – that’s really useful stuff I couldn’t find last time I looked. If anything, this kind of post will also help Google find stuff better…

    Thanks. For the arguments stuff, I had to go through the source code itself as there wasn’t any info available at all. Though I’d save everyone else the effort:-)

    Excellent ifelse. I’ve been putting off the Codex query_post page for too long, so I’m going to steal…err, adapt some of your posts for it (with proper attribution, naturally).

    “I’ve been putting off the Codex query_post page for too long, so I’m going to steal…er, adapt your post for some of it”
    By all means Kafkaesqui, go ahead. Saves me the work of doing so:-)

    Just for the benefit of those who don’t understand why you would use a second loop and why it would be more beneficial (if at all) over, say, using aplugin that sort of does the same thing and so on, coudl someone give a brief idea of what using a second loop would enable a user to do and how it can improve the site in terms of structure and so on?

    Sure Jinsan, here’s a quick intro.

    Query posts enable various advanced functionality without the use of plugins.

    Some examples of how I’ve exploited it on my site:

    • Retrieve the content for the latest post or even a WP “Page” and always show that on the top of your home page in one style. Underneath that, then show the usual chronological blog posts in a different style.
    • Show the usual blog posts in the main body. On the same page (perhaps on the sidebar), show miniposts/asides/shorts without the use of a plugin
    • Make the homepage only display the content that you want without affecting other pages i.e. you can show only the latest post on your home page but still show 10 posts in your archives, search, category pages
    • Make your archive page show all the entries without pagination.

    This list really only scratches the surface of what you can do. There are a few sites that I know of that demonstrate these features. There’s my own site:-), Root’s site, SomeFoolWithA and probably a few others which I’ve forgotten.

    Why not use a plugin to do this? As this is WP functionality, you can write a theme which will work without needing any plugins to be activated i.e. portability.

    In addition, WP provides you this functionality out of the box. Why not leverage it and keep things simple?

    Thread Starter infovore

    (@infovore)

    The original reason I was experimenting this was trying to use WordPress as a magazine CMS. On the front page, I wanted to pull one, latest article from each category and display it on the index.

    Which sounds like it should be an inbuilt thing – pull the latest from each category and display it. It is – it’s just it requires more than one loop. Posts in WordPress, for those of you used to other blogging systems, are requested by a query and looped through. More than one set of criteria for choosing a post on a page == more than one loop necessary.

    So, if you wanted your blog to be “every post I’ve made in every category” you’d have a normal loop, but to also show your most recent photo (something you’d categorised as a photo) in a sidebar, you’ll need a second loop. It’s just getting into the mindset: you ask for posts with a criteria, you loop through the result – and you’ll need several requests for several criteria.

    Why not a plugin? Exactly what ifelse says. This is *not* rocket science, and it’s something that requires a great deal of flexibility. I can think of writing a plugin to do this, but it’d be awfully inefficient. This is the cleanest, simplest most efficient way of doing it, and, as ifelse points out, it’s not something that needs a plugin – all that functionality is there waiting for you. And, of course, it’s less likely to break than a plugin when the next version’s released.

    I am not strictly speaking using multiple loops although that is being pedantic. I am using query post and then the loop. The way mine is set up it calls one post of my choice from one pre nominated category in the lead story section. Then it does the markup. Then it does the usual thing. You could then add a third query and call excerts say from your recent posts somewhere else. All on one page. No plugins needed and entry level php hoodickery only required. As to infovores comments. In theory WP might do a lot of things. But it doesnt. Consequently there is no bloat. Most of us are pretty happy with it. ??

    It certainly sounds curious though I probably won’t play with it at this stage as I’m studying other things, but it’s got me interested at the very least.

    infovore, have made any headway with creating a CMS style example of WP using the methods you mention, and thanks for the links ifelse, I tend to visit them often, I just wasn’t aware how they were achieved:)

    This multiple loop hack is great, but it breaks prev/next navigation. Is there any way to have prev/next work, and for each loop?

    This multiple loop hack is great, but it breaks prev/next navigation. Is there any way to have prev/next work, and for each loop?

    This is my question exactly. I’m running The Loop twice so I can show future postings in a group, and then past postings in a group.
    See the page I’m talking about here.
    The problem is when I reach the 10 post limit in total (both past & present), the prev/next stuff comes up. For the moment I’ve just made the limit larger, but I’d like to find a way to get this to work more cleanly instead of changing the global limit setting.

    Since I’m sorting by date after the loop gives the postings, is there anyway to tell the loop that I’d like x more please?

    Are you saying that even after setting query_posts for each rewound loop, the next/prev navigation breaks?

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘Howto: multiple loops in 1.5’ is closed to new replies.