WordPress Numeric Pagination

I’ve made quite a few WordPress themes in my time, and so I thought I would write a few blog posts sharing some of the code snippets that I find myself using repeatedly.

First off is some numeric pagination using native WordPress functionality. I have always prefered numbered pagination over next and previous links as it gives a good indication of progression and location – and allows you to skip around much more quickly – but WordPress doesn’t have a single line command for it.

For a long time I used my own custom function – but now I use the function below.

The function that does all the clever stuff is the paginate_links command. For WordPress it’s mostly used in the admin, but it’s easy to reuse as shown below.

/**
 * numeric pagination for custom queries
 * Much nicer than next and previous links :)
 *
 * @global type $wp_query
 * @param type $pageCount
 * @param type $query
 * @return type
 */
function bm_numeric_pagination( $page_count = 9, $query = null ) {

	if ( null == $query ) {
		global $wp_query;
		$query = $wp_query;
	}

	if ( 1 >= $query->max_num_pages ) {
		return;
	}

	$big = 9999999999; // need an unlikely integer

	echo '<div id="archive-pagination pagination">';
	echo paginate_links( array(
		'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
		'format' => '?paged=%#%',
		'current' => max( 1, get_query_var( 'paged' ) ),
		'total' => $query->max_num_pages,
		'end_size' => 0,
		'mid_size' => $page_count,
		'next_text' => __( 'Older ›', 'textdomain' ),
		'prev_text' => __( '‹ Newer', 'textdomain' )
	) );
	echo '</div>';

}

How was it for you? Let me know on BlueSky or Mastodon

(Please) Link to this page

Thanks for reading. I'd really appreciate it if you'd link to this page if you mention it in your newsletter or on your blog.

Related Posts

31 Oct 2013

WordPress – Estimated Reading Time

I have a theme coming out soon that displays the ‘estimated reading time’ for each blog post on the homepage.The theme is inspired by the new blogging service Medium – but I first saw the idea mentioned on Brian Crays...
26 Sep 2018

WordPress get_post_gallery() Gutenberg Polyfill

I’m working on a new WordPress theme designed for Gutenberg, however Gutenberg isn’t finished, and there are elements that won’t work until the plugin is merged with core.One such issue, that I ran into this weekend, is that get_post_gallery() doesn’t...
10 Apr 2015

Adding Menu Descriptions to WordPress Menus

In WordPress there’s an option to add custom descriptions to Menu links – but by default there’s no way to display these descriptions. I’m currently redesigning Binary Moon and wanted to add descriptions to the menu – so I thought...
06 Apr 2015

Disabling Website URLs in WordPress Comments

These days a lot of spammers submit spam comments that are perfectly legitimate apart from the fact that they link somewhere. So I thought I would disable the website url field in the comments so that comments can focus on...
01 Dec 2013

WordPress Improved Human Time Difference

WordPress has a built in function, called human_time_diff, for creating messages that say how long ago a post was published – however I think it is confusing when you have posts from a long time ago that read “posted 15...
22 Nov 2014

WordPress 4.1 Improvements for Theme Developers

WordPress 4.1 is bringing with it a couple of cool new additions for theme developers. They’re things that are currently a bit messy to implement in themes. For me they are things that I do the same way in all...