I’ve added pagination for a custom post type within my WordPress theme. It works great, apart from it shows too many pages in the pagination menu for the number of posts: http://www.electrickiwi.co.uk/testimonials/
There should currently be 7 pages, but it’s showing 12. The code below is what I am using to display the pagination. There is nothing in the functions.php file relating to this.
<?php
/* ------------------------------------------------------------------*/
/* PAGINATION */
/* ------------------------------------------------------------------*/
//paste this where the pagination must appear
global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ($total > 1) {
// get the current page
if (!$current_page = get_query_var('paged')) {
$current_page = 1;
}
// structure of "format" depends on whether we're using pretty permalinks
if (get_option('permalink_structure')) {
$format = 'page/%#%/';
}
else {
$format = 'page/%#%/';
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Remove the
'total' => $total
part from the array you pass to paginate_links so the number of pages is automatically calculated.I fixed this by changing ‘
total' => $total
to ‘total' => $dataQuery->max_num_pages,
I was having this exact same issue, but was able to solve it by using ‘total’ => $my_query->max_num_pages, like this: