Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End help understanding how to do multiple wordpress loops on one page

  • This topic is empty.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #31235
    clockwork
    Member

    hello all,

    my index page has multiple categories on there.. i want each category to display it’s new posts in the corresponding section/list. i’ve looked at codex but the stuff on there is way over my head. can someone walk me through this or have a really simplified site for help?

    thanks..

    #66209
    Rob MacKay
    Participant

    If you Google WP Mutipul loops you will find quite a few tutorials on it.

    Make sure it is semi recent as some of thing things have changed a little (I don’t think you have to rewind the loop any more)…

    here is an intro to the loop too – which is always good to know:

    https://css-tricks.com/video-screencasts/91-the-wordpress-loop/

    :D

    #66145
    clockwork
    Member

    thanks robski.

    yeah, i see alot of them and some are really old. what would be nice is to actually see it in action in code. i don’t know where the code goes in the page.

    #66148
    Johnnyb
    Member

    It’s just the same as using a single loop except you have more than one! So for example if you wanted 3 columns each with a different category of posts you’d use something like the following:



    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>



    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>



    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>

    You just change the category within query_posts to correspond with the ID number of whichever category you’d like to display posts from. Obviously those are very simple loops for the sake of example.

    #65818
    Rob MacKay
    Participant

    Just make sure you reset the query at the end too



    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif;
    reset_query();
    ?>

    if you don’t I think you basically just add to it instead of starting fresh :)

    #65122
    cjk
    Member

    Hey Robski, how about the $my_query = new WP_Query() method and the get_post() method? I know the new WP_Query eliminates rewind and reset, I use that on my site to list posts from specific categories on certain related page pages. The get_post() method I haven’t played with yet but apparently it eliminates multiple query issues as well.

    As usual though, in WordPress there are numerous ways to get something done and this leads to confusion. I wish I could find an article explaining why and when you would use the different methods!

    #65067
    mshort1985
    Member

    Chris did a screencast about this a while back, it can be found here. He goes through how to use the loop generally as well as how to make multiple loops on the same page.

    #84877
    slobjones
    Member

    Robski, adding your reset_query line results in: “Fatal error: Call to undefined function reset_query()”

    I know we need to reset the loop. But what is the correct procedure?

    Thanks.

    #84882
    slobjones
    Member

    Another question for JohnnyB:

    Using your script, how would we prevent duplicate posts?

    I’ve tried to apply the appropriate code shown in the Codex Loop discussion, but end up knocking out an entire category instead.

    Thanks.

    #84887
    jayme
    Member

    Nice thread guys! We might try to work a reference to this one into our blog post about wordpress houses later next week. You can get answers of all of your queries there. http://juggernautwebsdesign.com/wordpress%5Dwordpress company

    #84901
    Brightonmike
    Member

    @jayme You do know this forums links are nofollow, right? As in, you will gain absolutely NO SEO benefit from posting links in here

    #84903
    krogsgard
    Member

    Actually, you don’t want to use query_posts to do multiple loops on one page. WP_Query is the most versatile solution. The best article on the subject of when / how to use which loop method is actually on CSS-Tricks’ sister site, DigWP.

    It really is a must read: http://digwp.com/2011/05/loops/

Viewing 12 posts - 1 through 12 (of 12 total)
  • The forum ‘Back End’ is closed to new topics and replies.