Create Custom Pagination Function in WordPress theme

Posted by
December 24, 2018

Sometime you want your custom style to be implemented in wordpress pagination for this you want to have your own pagination func

For that add this fun to your theme functions.php file

 

function my_pagination($numofpages = '', $pagerange = '', $paged = '') {
    if (empty($pagerange)) {
        $pagerange = 2;
    } global $paged;
    if (empty($paged)) {
        $paged = 1;
    } if ($numofpages == '') {
        global $wp_query;
        $numofpages = $wp_query->max_num_pages;
        if (!$numofpages) {
            $numofpages = 1;
        }
    } 
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numofpages,
'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 " . $numofpages . "</span> "; echo '<ul class="pagination">'; foreach ($paginate_links as $page) { echo "<li>$page</li>"; } echo '</ul>'; echo "</div>"; } }

After adding the function move to the page where you have the post loop and the code above the loop

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

After this the final step is to call the defined fun for this after the end of loop add the following code

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

 

read more