WordPress Custom Pagination functions.php; Without a Plugin

Image for post
Image for post
Wordpress Pagination
function custom_pagination($numpages = '', $pagerange = '', $paged='') {

if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}

$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' => 'array',
'add_args' => false,
'add_fragment' => ''
);

$paginate_links = paginate_links($pagination_args);

if (is_array($paginate_links)) {
echo "<div class='cpagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo '<ul class="pagination">';
foreach ( $paginate_links as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
echo "</div>";
}
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
if (function_exists(custom_pagination)):
custom_pagination($the_query->max_num_pages,"", $paged);
endif;
wp_reset_query();

Written by

Tech Enthusiast

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store