• This code works for me, but I am trying to stop using query_posts because its supposed to be not good.

    <?php query_posts('posts_per_page=2'); ?>
    <?php if ( have_posts() ) : ?>
    <?php while ( have_posts() ) : the_post();  ?>
    
    <!-- stuff -->
    
    <?php endwhile; ?>
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php else : ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    so I rewrote it as this, but the pagination doesn’t work.

    <?php $the_query = new WP_Query( array( 'posts_per_page' => 2, 'pagination' => true, ) ); ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <!-- stuff -->
    
    <?php endwhile; ?>
    
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php else : ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    Help?

Viewing 1 replies (of 1 total)
  • Thread Starter mrspabs

    (@mrspabs)

    i think i figured it out. can someone confirm that I am right?

    <?php  $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;  ?>
    <?php $the_query = new WP_Query( array( 'posts_per_page' => 2, 'pagination' => true, )); ?>
    <?php if ( $the_query->have_posts() ) : ?>
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
    <!-- stuff -->
    
    <?php endwhile; ?>
    
    <div class="nav-previous alignleft"><?php next_posts_link('Older posts', $the_query->max_num_pages ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    <?php else : ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    The code works, but I want to be sure that its all written correctly since I am trying to fix bad habits.

Viewing 1 replies (of 1 total)

The topic ‘loop with the_query instead of query_posts – with pagination’ is closed to new replies.