[resolved] Previous/Next Links when using Multiple Loops? (3 posts)

  1. meko
    Posted 9 years ago #

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

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

    Any suggestions?

    thx- Melanie

  2. 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
    query_posts($query_string . "&cat=2");
    while (have_posts()) : the_post(); // the second loop
    ... 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.

  3. meko
    Posted 9 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic