In this tutorial I am going to demonstrate the way to customize the WordPress default pagination using the custom WordPress WP_Query (the Query class) loop.
Here’s a sample of WP_Query :
/** * @param string $numpages * @param string $pagerange * @param string $paged */ function custom_pagination($numpages = '', $pagerange = '', $paged='', $base = '', $type='') { if ($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; } } $base = ($base) ? : get_pagenum_link(1); /** * We construct the pagination arguments to enter into our paginate_links * function. */ $pagination_args = array( //'base' => get_pagenum_link(1) . '%_%', 'base' => $base.'%_%', 'format' => 'page/%#%', 'total' => $numpages, 'current' => $paged, 'show_all' => False, 'end_size' => 1, 'mid_size' => $pagerange, 'prev_next' => True, 'prev_text' => __('«'), 'next_text' => __('»'), 'type' => 'list', 'add_args' => false, 'add_fragment' => '', ); $paginate_links = paginate_links($pagination_args); $paginate_links = str_replace('<ul class="page-numbers"', '<ul class="pagination justify-content-center"', $paginate_links); $paginate_links = str_replace("<ul class='page-numbers'", '<ul class="pagination justify-content-center"', $paginate_links); $paginate_links = str_replace('<li>', '<li class="page-item">', $paginate_links); $paginate_links = str_replace('<li class="page-item"><span aria-current="page" class="page-numbers current"', '<li class="page-item active"><span class="page-link" aria-current="page"',$paginate_links); $paginate_links = str_replace('<a class="page-numbers"', '<a class="page-link"', $paginate_links); $paginate_links = str_replace('<a class="next page-numbers"', ' <a class="page-link is-next"', $paginate_links); $paginate_links = str_replace('<a class="prev page-numbers"', '<a class="page-link is-previous"', $paginate_links); echo $paginate_links; } $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; $args = [ 'post_type' => 'post', 'paged' => $paged,'posts_per_page' => 6, 'post_status'=> 'publish']; $query = new WP_Query($args); $actionUrl = get_permalink(); if ($query->have_posts()): while ($query->have_posts()): $query->the_post(); the_title('<h2>','</h2>'); the_content(); endwhile; custom_pagination($query->max_num_pages,"",$paged, $actionUrl); endif;
Views: 7