-3

I'm trying to get pagination working on my custom wp_query post loop. I currently have it set up, but when I click on page 2, it shows the exact same posts from page 1. I have a total of about 8 posts and the limit set to 5 posts per page. Below you can see the function found in my functions.php file

if ( !function_exists( 'wpex_pagination' ) ) {

function wpex_pagination() {

$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';

global $wp_query,$the_query;

if ( $the_query ) {
  $total = $the_query->max_num_pages;
  } else {
  $total = $wp_query->max_num_pages;
  }
  $big = 999999999; // need an unlikely integer
    if( $total > 1 )  {
         if( !$current_page = get_query_var('paged') )
             $current_page = 1;
         if( get_option('permalink_structure') ) {
             $format = 'page/%#%/';
         } else {
             $format = '&paged=%#%';
         }
        echo paginate_links(array(
            'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
            'format'        => $format,
            'current'       => max( 1, get_query_var('paged') ),
            'total'         => $total,
            'mid_size'      => 3,
            'type'          => 'list',
            'prev_text'     => $prev_arrow,
            'next_text'     => $next_arrow,
      ) );
    }
  }
}

and then below is the loop on my template page page-news.php

<?php
        $the_query = new WP_Query( 'posts_per_page=5' );
        if ($the_query -> have_posts()) :
        while ($the_query -> have_posts()) : $the_query -> the_post();
      ?>

      <div class="row blog-excerpts">
        <div class="col-md-4">
          <a href="<?php the_permalink(); ?>" class="angled-img">
            <div class="img">
              <?php the_post_thumbnail(); ?>
            </div>
          </a>
        </div>

        <div class="col-md-8">
          <div class="clearfix">
            <h3 class="h2 pull-left">
              <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </h3>
            <span class="date pull-right">
              <span class="glyphicon glyphicon-calendar"></span>
              <?php the_time( get_option( 'date_format' ) ); ?>
            </span>
          </div>

          <div class="description">
            <?php the_excerpt(); ?>
          </div>

          <a href="<?php the_permalink(); ?>" class="btn">Read More</a>
        </div>
      </div>
      <?php endwhile; wp_reset_postdata(); wpex_pagination(); endif;  ?>

any help or pointers in to the right direction is very much appreciated. I've been trying to get this working for a few days now and have read over numerous articles on the internet.

0

You need to add the paged parameter to your WPQuery:

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$query = new WP_Query( array( 'posts_per_page' => 5, 'paged' => $paged ) );

Here are the docs.

  • Wow, I can't believe it was that simple. For some reason I thought that paged was something already set by default so I didn't need to set it. Really glad you got that cleared up for me, it was getting pretty frustrating. – Chris Short Jun 30 '16 at 15:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.