Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Wordpress pagination issue - showing same posts on each page

Featured Replies

Basically, I'm trying to use paginate_links to get paging on my posts, and after lots of Google searches, I've ended up with the code below:

 

<?php
// Get standard page content
while ( have_posts() ) : the_post();
 the_content();
endwhile;

// Display 5 posts from "whats on" category
$my_query = new WP_Query('category_id=5');
while ($my_query->have_posts()) : $my_query->the_post(); ?>
 <div class="whats_on_item clearfix">
 <?php $do_not_duplicate = $post->ID; ?>
 <a href="<?php the_permalink(); ?>"><img src="<?php the_field( 'thumbnail' ) ?>" /></a>
 <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
 <em><?php the_date(); ?></em><br>
 <?php echo get_the_excerpt(); ?>
 </div>
<?php endwhile; ?>

<div class="post_paging">
<?php
$big = 999999999; // need an unlikely integer

 echo paginate_links( array(
 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
 'format' => '?paged=%#%',
 'current' => max( 1, get_query_var('paged') ),
 'total' => $my_query->max_num_pages
 ) );
?>

 

The above displays the pagination absolutely fine, however it is showing the same posts on each page (kinda defeating the point of pagination!). Searching the net, I have tried several suggestions, however I am a relative newbie to Wordpress and PHP so I could really use some help here.

 

Any help much appreciated!

  • Author

SOLVED

 

Had to modify WP_Query like so:

 

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query('category_id=5&paged=' . $paged);

 

Working code as follows:

 

// Display 5 posts from "whats on" category
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$my_query = new WP_Query('category_id=5&paged=' . $paged);
while ($my_query->have_posts()) : $my_query->the_post(); ?>
 <div class="whats_on_item clearfix">
 <?php $do_not_duplicate = $post->ID; ?>
 <a href="<?php the_permalink(); ?>"><img src="<?php the_field( 'thumbnail' ) ?>" /></a>
 <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
 <em><?php the_date(); ?></em><br>
 <?php echo get_the_excerpt(); ?>
 </div>
<?php endwhile; ?>

<div class="post_paging">
<?php
$big = 999999999; // need an unlikely integer

 echo paginate_links( array(
 'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
 'format' => '?paged=%#%',
 'current' => max( 1, get_query_var('paged') ),
 'total' => $my_query->max_num_pages
 ) );
?>

 

Amazing what a good nights sleep does :-)

  • Author

Another issue now - can't get pagination to work on the search.php page. Copied pretty much the same code as above:

 

<?php
   while ( have_posts() ) : the_post(); ?>
 <div class="whats_on_item clearfix">
  <?php $do_not_duplicate = $post->ID; ?>
  <a href="<?php the_permalink(); ?>"><img src="<?php the_field( 'thumbnail' ) ?>" /></a>
  <h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
  <div class="date"><?php the_date(); ?></div>
  <?php echo get_the_excerpt(); ?>
 </div>
   <?php endwhile;  ?>

   <?php if ( $my_query->max_num_pages > 1 ) : ?>
   <div class="post_paging">
   <?php
   $big = 999999999; // need an unlikely integer

 echo paginate_links( array(
  'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
  'format' => '?paged=%#%',
  'current' => max( 1, get_query_var('paged') ),
  'total' => $my_query->max_num_pages
 ) );
   ?>
   </div>
   <?php endif; ?>

 

The pagination doesn't show at all. If someone thinks they can help, I'll happily PM you a link to the site.

 

Any help appreciated

Need to make sure the search query is paged again. Try this in functions.php (off the top of my head so not tested, but should work):

 

add_action('pre_get_posts', 'rd_paginate_query');

function rd_paginate_query($query = false) {
 if (!is_a($query, 'WP_Query') || !$query->is_main_query()) {
   return;
 }
 $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
 $query->set('paged', $paged);
}

Edited by Renaissance-Design

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.