treehouse : what would you like to learn today?
Web Design Web Development iOS Development

help understanding how to do multiple wordpress loops on one page

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

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

    :D
  • 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.
  • 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:



    <div id="column-1">
    <?php query_posts('cat=1&posts_per_page=5');
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>
    </div>

    <div id="column-2">
    <?php query_posts('cat=2&posts_per_page=5');
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>
    </div>

    <div id="column-3">
    <?php query_posts('cat=3&posts_per_page=5');
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>
    </div>



    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.

  • Just make sure you reset the query at the end too


    <div id="column-3">
    <?php query_posts('cat=3&posts_per_page=5');
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif;
    reset_query();
    ?>
    </div>


    if you don't I think you basically just add to it instead of starting fresh :)
  • 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!
  • 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.
  • 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.
  • 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.
  • 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]wordpress company
  • @jayme You do know this forums links are nofollow, right? As in, you will gain absolutely NO SEO benefit from posting links in here

  • 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/