WordPress.org

Ready to get started?Download WordPress

Forums

Howto: multiple loops in 1.5 (23 posts)

  1. infovore
    Member
    Posted 9 years ago #

    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

  2. ColdForged
    Member
    Posted 9 years ago #

    Excellent. There's a similar resource available on the Codex, but more resources is always good.

  3. ifelse
    Member
    Posted 9 years ago #

  4. ifelse
    Member
    Posted 9 years ago #

    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.)

  5. infovore
    Member
    Posted 9 years ago #

    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...

  6. ifelse
    Member
    Posted 9 years ago #

    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:-)

  7. Kafkaesqui

    Posted 9 years ago #

    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).

  8. ifelse
    Member
    Posted 9 years ago #

    "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:-)

  9. Jinsan
    Member
    Posted 9 years ago #

    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?

  10. ifelse
    Member
    Posted 9 years ago #

    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?

  11. infovore
    Member
    Posted 9 years ago #

    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.

  12. Root
    Member
    Posted 9 years ago #

    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. :)

  13. Jinsan
    Member
    Posted 9 years ago #

    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:)

  14. Arlo
    Member
    Posted 9 years ago #

    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?

  15. mcmartin
    Member
    Posted 9 years ago #

    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?

  16. neetij
    Member
    Posted 9 years ago #

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

  17. mcmartin
    Member
    Posted 9 years ago #

    In this case I'm just doing a rewind_posts() and then running The Loop again. Since it's top category page, only posts from that category come out. If either the first or second running of the loop generates a second page, then BOTH the first and second calls to next/prev navigation return a next link. I don't yet know why, but if I try a query_posts from within this category page I get a database error (it works fine on my home.php page).

  18. moshu
    Member
    Posted 9 years ago #

    OK, maybe somebody can help.
    I am on halfway :)
    Using the query_posts I can pull up the latest from a certain category and put it in a prominent place at the top on the home.php.
    However if I have further the "normal" Loop the post is repetead there in where it should be chronologically. How can I eliminate it? Use another query? If you could point me to the right direction...
    Thanks.

  19. Root
    Member
    Posted 9 years ago #

    You need to offset. But dont ask me how. What I did instead was nominated dedicated cats for those two types of fromt page posts.

  20. moshu
    Member
    Posted 9 years ago #

    1. I remember the "offset" from Wp1.2. It was based on the old Loop (foreach). Not sure if it works with 1.5.x.x
    2. I guess I am slow today: I understand having a "dedicated" category for the posts I want with the query_posts. But what is the other of the "two types"?

  21. Root
    Member
    Posted 9 years ago #

    I used one cat for my single post. And one cat for my four other posts for the front page. Then I just reallocated as required.

  22. moshu
    Member
    Posted 9 years ago #

    Anybody has an idea how to offset the loop not to show again the post from the query?

  23. moshu
    Member
    Posted 9 years ago #

    For the benefit of others.
    There is an answer posted by Kafkaesquí in another thread:
    http://wordpress.org/support/topic/28203#post-193667

Topic Closed

This topic has been closed to new replies.

About this Topic