Grow your CSS skills. Land your dream job.

help understanding how to do multiple wordpress loops on one page

  • # January 13, 2011 at 2:45 am

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

    Rob
    # January 13, 2011 at 4:27 am

    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

    # January 13, 2011 at 11:31 am

    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.

    # January 13, 2011 at 12:44 pm

    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:



    < ?php query_posts('cat=1&posts_per_page=5');
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>



    < ?php query_posts('cat=2&posts_per_page=5');
    if (have_posts()) : while (have_posts()) : the_post();
    the_content();
    endwhile; endif; ?>



    < ?php query_posts('cat=3&posts_per_page=5');
    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.

    Rob
    # January 14, 2011 at 6:53 pm

    Just make sure you reset the query at the end too



    < ?php query_posts('cat=3&posts_per_page=5');
    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 :)

    cjk
    # January 19, 2011 at 10:24 pm

    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!

    # January 19, 2011 at 10:52 pm

    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.

    # August 11, 2011 at 10:54 pm

    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.

    # August 12, 2011 at 12:04 am

    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.

    # August 12, 2011 at 7:31 am

    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/wordpresswordpress company

    # August 12, 2011 at 7:40 am

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

    # August 12, 2011 at 7:51 am

    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)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".