WordPress.org

Ready to get started?Download WordPress

Forums

Two loops, broken pagination (4 posts)

  1. andykarasek02
    Member
    Posted 1 year ago #

    Here I have two loops each displaying 5 posts, this all works seemingly well until the navigation is brought into the equation, unfortunately far back you go in blog posts the second loop still displays the same 5 posts on every page.

    I have read through the codex and added bits to the code (like paged) in the second loop but it still doesn't seem to help

    <?php
    global $query_string;
    $posts = query_posts($query_string.'posts_per_page=5') ?>
    <?php while ( have_posts() ) : the_post() ?>
    <?php the_content(); ?>
    <?php endwhile; ?>
    <?php posts_nav_link(); ?>
    
    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string.'&showposts=5&offset=5&paged='.$paged); ?>
    <?php while ( have_posts() ) : the_post() ?>
    <?php the_content(); ?>
    <?php endwhile; ?>		
    
    <?php posts_nav_link(); ?>

    What I am looking for is for both loops to go back/forward when clicking 'next/previous page'
    any assistance appreciated.

  2. vtxyzzy
    Member
    Posted 1 year ago #

    It can be quite complicated to paginate two separate loops.

    Both of your loops look the same. Perhaps what you want to do can be accomplished with a single loop and a counter.

    Can you describe what you are trying to do with the separate loops?

  3. andykarasek02
    Member
    Posted 1 year ago #

    I see, yes they are basically the same other then the offset.
    I just want to have them in separate divs for a column layout

  4. vtxyzzy
    Member
    Posted 1 year ago #

    Here is sample code showing how to display posts in two columns, newspaper style:

    <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    query_posts($query_string.'&posts_per_page=10&paged='.$paged);
    $posts_per_column = ceil(sizeof($wp_query->posts) / 2); ?>
    
    <div class='left-column'>
    <?php while ( have_posts() ) : the_post();
       if ( $post_count++ == $posts_per_column ) {
          echo "\n</div><!-- .left-column -->\n<div class='right-column'>\n";
       }
       // Display your post here
    endwhile;
    echo "\n</div><--! end columns -->\n";

Topic Closed

This topic has been closed to new replies.

About this Topic