This is the pagination function I use.
I’m not sure if the github link is the original credit, but its one i found.
Add the following to your functions.php file:
/**
* Creates Custom Pagination
* @link https://gist.github.com/rgfx/755cfb71fd1732e4e7b7bdcbd4b6e4e3
* @param string $numpages Show pagination in WP_Query
* @param string $pagerange Show pagination in WP_Query
* @param string $paged Show pagination in WP_Query
* @return html
*/
function custom_pagination($numpages="", $pagerange="", $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
/**
* This first part of our function is a fallback
* for custom pagination inside a regular loop that
* uses the global $paged and global $wp_query variables.
*
* It's good because we can now override default pagination
* in our theme, and use this function in default quries
* and custom queries.
*/
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
/**
* We construct the pagination arguments to enter into our paginate_links
* function.
*/
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __(''),
'next_text' => __(''),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class="custom-pagination">";
echo "<span class="page-numbers page-num">Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
}’
Then, inside your archive template, add this right before your wp_reset_postdata(); :
<!-- pagination here -->
<?php if (function_exists(custom_pagination)) {
custom_pagination($post_query->max_num_pages,"",$paged);
} ?>
Also, it may depend on what page template you are on. Im assuming a custom template or an archive page template.
Next, if youre still having trouble, then id suggest checking your $paged variable, like so:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$suri = $_SERVER['REQUEST_URI'];
// check $paged variable
echo '<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">';
print_r($paged);
echo '</pre>';
// grab the last number from the url to use as the $paged variable
$exp_suri = explode("https://wordpress.stackexchange.com/",$suri);
$page_query = $exp_suri[2];
$page_num = substr($page_query, -1);
$paged = is_numeric($page_num) ? $page_num : $paged;
// Now, check $paged variable again
echo '<pre style="display:none; text-align: left; overflow:visible!important; z-index: 10000; position: relative;">';
print_r($paged);
echo '</pre>';
You can probably remove or comment out the second part ($exp_suri to the end) to begin.
The pre tags here are display:none, so either take that out of the inline style, or show it using dev tools.
Finally, if youre still stuck, id read the WordPress pagination trouble shooting, here.