WordPress.org

Support

Support » How-To and Troubleshooting » Multiple Loops “Previous Entries” Problem

Multiple Loops “Previous Entries” Problem

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

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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.

    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; ?>

    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.

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

    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.

    Moderator Michael

    @alchymyth

    Forum Moderator

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

    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?

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Multiple Loops “Previous Entries” Problem’ is closed to new replies.