WordPress Custom Pagination functions.php; Without a Plugin

Bikram KC
2 min readJul 30, 2017
Wordpress Pagination

WordPress Pagination is a very essential part for the front, category or archive pages, so you will require you hands on skill on pagination, but it is not as easy as it seems and the documentation for the pagination is not that straightforward if you want page numbers.

Adding the following code into functions.php

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>";
}

And call the function to wherever you want like this:
You should have the $paged variable like below before The Loop:

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;

And after End of The Loop after endwhile;, add the following snippet code:

if (function_exists(custom_pagination)):
custom_pagination($the_query->max_num_pages,"", $paged);
endif;
wp_reset_query();

This is how you can minimize the number of plugins in your wordpress website.

--

--