WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Loops "Previous Entries" Problem (8 posts)

  1. aptdesignonline
    Member
    Posted 5 years ago #

    Hi. I've setup 2 loops on my homepage, each displays 5 posts, but a little differently. I got the code to do this from: WP Recipes. You can see the site I'm working on here: Wenatchee Fitness Blog

    The loops work well, however when someone clicks "Previous Entries" at the bottom, page2 just shows the same 10 entries.

    Any ideas on how this could be fixed? The code I'm using is below:

    <!--  First loop, we only get the most recent post-->
    <?php query_posts('showposts=5'); ?>
    <?php if (have_posts()) : ?>
    <?php     while (have_posts()) : the_post(); ?>
    
    ...display the most recent 5 posts in here...
    
    <?php endwhile;  ?>
    <?php endif; ?>
    
    <!--  Second loop, we get 5 posts excluding the most recent.-->
    <?php query_posts('showposts=5&offset=5');  ?>
    <?php if (have_posts()) : ?>
    <?php     while (have_posts()) : the_post(); ?>
    
    ...display the next 5 posts in here...
    
    <?php endwhile; ?>
    <?php endif; ?>

    With all of the content inside the ...display... section.

    Thanks in advance for your help!

  2. Shane G
    Member
    Posted 5 years ago #

    Hi,

    Refer this article and search for your previous links details in this article:

    http://codex.wordpress.org/The_Loop_in_Action

    Thanks,

    Shane G.

  3. aptdesignonline
    Member
    Posted 5 years ago #

    Thanks for the link Shane. I checked out that article and the only difference I could find between what I had and what it said was that my navigation needed to be included outside The Loop, but inside the if condition.

    I changed that but still have the same problem. Any other ideas?

    My code for the nav now looks like this:

    `
    <?php endwhile; ?>

    <div id="nav-below" class="navigation">
    <div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">«</span> Older posts', 'ggblog' )) ?></div>
    <div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">»</span>', 'ggblog' )) ?></div>
    </div>

    <?php endif; ?>

  4. 808thresholdgate
    Member
    Posted 5 years ago #

    I have the same problem. I'm using pages and querying by category inside them. The "older entries" link just gives me the same latest posts.

  5. rmsylte
    Member
    Posted 4 years ago #

    I've got sites with the same problem. Any fixes?

  6. vtxyzzy
    Member
    Posted 4 years ago #

    I am just guessing, but could the problem be with having multiple loops?

    As I understand it, WP_Query retrieves a set of posts. the next_posts_link and previous_posts_link just navigate inside those sets. The second loop would get a new set, making the first set no longer available, and causing the links to start over by retrieving the first post of their set.

    Maybe some WordPress guru can confirm this.

  7. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    from the codex:

    The wp_query object will generate a new SQL query using your parameters. When you do this, WordPress ignores the other parameters it receives via the URL (such as page number or category). If you want to preserve that information, you can use the $query_string global variable in the call to query_posts().

    For example, to set the display order of the posts without affecting the rest of the query string, you could place the following before The Loop:

    global $query_string;
    query_posts($query_string . "&order=ASC");

    When using query_posts in this way, the quoted portion of the argument must begin with an ampersand (&).

    and also add the paged variable to your other things:

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string.'&showposts=5&paged='.$paged);
  8. vtxyzzy
    Member
    Posted 4 years ago #

    OK, but I still don't understand how multiple loops would work. When you click on one of the next or previous links, won't it generate a URL with the correct page number for that loop? What does the other loop do?

Topic Closed

This topic has been closed to new replies.

About this Topic