[resolved] Previous/Next posts links stop working while using multiple loops (4 posts)

  1. sequence
    Posted 10 years ago #

    I am using three loops in my index. This goes well and they all seem to work on first sight.

    // /////start of loop code//////////////////////////

    //first loop makes 2 excerpts!
    <?php query_posts('showposts=2');
    if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div class="post">
    <h2 id="post"><?php the_title(); ?></h2>
    <div class="entry">
    <?php the_excerpt() ?>
    <?php endwhile; endif; ?>

    //second loop list the 10 most recent posts
    <h2>Last 10 Posts</h2>
    <?php query_posts('showposts=10'); ?>
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    [<?php the_time('d-m') ?>]</td><td><?php the_time('H:i') ?></td><td><?php the_title() ?> (<?php comments_aantal();?>)
    <?php endwhile; endif; ?>

    //third loop shows the 10 articles
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="post">
    <h2 id="post"><?php the_title(); ?></h2>
    <small><?php _e("Written by"); ?> <?php the_author() ?> in <?php the_category(',') ?> — <?php the_time('l j F Y') ?> <?php the_time('G:i') ?> <?php edit_post_link(__('[edit]')); ?></small>
    <div class="entry">
    <?php the_content('More.. »'); ?>
    <?php comments_popup_link('No reactions »', '1 reaction »', '% reactions »'); ?>
    <?php endwhile; ?>

    // ////////end of loop code///////////////////////////

    Everything is showed as i want is (on http://www.x-ite.nl), however when clicking on 'Vorige berichten' (Previous posts) at the bottom of my page I'm going to http://www.x-ite.nl/page/2/.
    The problem is that over there the same posts are shown, instead of the 10 previous posts!

    The code I used to link to previous posts is:
    // ////////start of previous post code/////////////////
    <div class="navigation">
    <div class="alignleft"><?php posts_nav_link('','','« Vorige berichten') ?></div>
    <div class="alignright"><?php posts_nav_link('','Volgende berichten »','') ?></div>
    // ////////end of previous post code///////////////////

    What do I need to change so that I can fix the problem of showing the same posts (on what should be different pages)?
    I already read http://codex.wordpress.org/The_Loop#Multiple_Loops but as they say over there, query_posts should be used (as I already do..)

    I'm using the theme Kubrick-nl 1.5 and WP 2.0.2.
    When i remove the first 2 loops, the previous post links work fine.

  2. maerk
    Posted 10 years ago #

    I found a similar thing. When you use query_posts() it forgets all the other query variables that have been set, like if you are viewing page 1 or 2, or a particular category, or whatever.

    You can check for these using get_query_var() or you can just change the query variable you want to change directly:

    $wp_query->query_vars["showposts"] = 2;


    $wp_query->query_vars["showposts"] = 10;

    Use that instead of query_posts();.

  3. sequence
    Posted 10 years ago #

    Thanks! That solved the problem indeed!

  4. Seaborn
    Posted 10 years ago #

    I think this should be the answer to my problem but it's not pulling up any posts at all.

    This is what I am using

    <?php $wp_query->query_vars["showposts"] = 10;
    load_template( TEMPLATEPATH . '/index.php'); //loads index

    I am using this as a template as this site recomends


    - under using WordPress as a CMS

    Everything works fine but the previous / next pages. It links to page2 but pulls up the same posts.


    If anyone can help I would greatly appreciate it.

Topic Closed

This topic has been closed to new replies.

About this Topic