WordPress.org

Support

Support » Themes and Templates » [Resolved] Previous/Next Links when using Multiple Loops?

[Resolved] Previous/Next Links when using Multiple Loops?

  • I am using 5 loops on my index page, each pulling the last post from a different category.

    Next/previous links go to page 2 but show the same (most recent) posts again.

    I searched the forum, and the only solution I could find that addressed this same problem suggested using get_posts instead of query_posts in the loop, but I am already using get_posts.

    Here is the code for one of the loops (the rest are the same, except for category id):

    <?php
    $lastposts = get_posts('numberposts=1&category=5');
    foreach($lastposts as $post) :
    setup_postdata($post);
    ?>
    <?php the_content(); ?>
    <?php endforeach; ?>
    <!--end post-->

    Any suggestions?

    thx- Melanie

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    @otto42

    WordPress.org Tech Dude

    You got it backwards. Don’t use get_posts. Use query_posts with the query string and override the category.

    query_posts($query_string . "&cat=1");
    while (have_posts()) : the_post(); // the first loop
    ...
    endwhile;
    
    query_posts($query_string . "&cat=2");
    while (have_posts()) : the_post(); // the second loop
    ...
    endwhile;
    ... and so on ...

    The problem is that you want pagination to work, but you’re completely ignoring the query_string, which tells the query functions, among other things, what page of posts to display.

    Note that if you’re displaying one post per query, you will need to set the Options->Reading to only show one post instead of the default 10. This will make the loops each pull 1 post per page. So you’ll get the last 5 of each category (since that’s what you’re doing) and then page two will get the one before that in each category and so on.

    Perfect solution, worked like a charm.

    In addition, after changing the Options->Reading to only show one post, I used the Custom Query String plugin to set the archives pages to show more than just one post.

    Many thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘[Resolved] Previous/Next Links when using Multiple Loops?’ is closed to new replies.