Numbered page navigation for WordPress with Thesis

I wanted to share my code for numbered page navigation in Thesis. I started with this numbered pages tutorial, but wound up modifying it to add smart first/last and prev/next buttons.

You can see it in action on Caveat Emptor, Lawyerist, and Bitter Lawyer.

Here is what to plug into your custom.css file:

/* PAGE NUMBERING */

.page-nav {
	font-weight:bold;
	margin-left:2.2em;
	margin-top:1.1em;
}

.page-nav-intro,
.page-number a,
.current-page-number,
.ellipses {
	border-radius:5px;
	float:left;
	padding:.5em .7em;
}

.page-nav-intro {
	background:#eee;
	margin-right:1.1em;
}

.page-number a {
	background:#eee;
	margin:0 .2em;
}

.page-number a:hover {
	background:#bbb;
}

.current-page-number {
	background:#ddd;
	font-weight:bold;
	margin:0 .2em;
}

And here’s what to plug into your custom_functions.php file:

/* PAGE NUMBERING */

function numbered_page_nav($prelabel = '', $nxtlabel = '', $pages_to_show = 4, $always_show = false) {
	global $request, $posts_per_page, $wpdb, $paged;

	$custom_range = round($pages_to_show/2);
	if (!is_single()) {
		if(!is_archive() || is_author()) {
			preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);
		}
		else {
			preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
		}
		$blog_post_count = $matches[1];
		$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $blog_post_count");
		$max_page = ceil($numposts /$posts_per_page);
		if(empty($paged)) {
			$paged = 1;
		}
		if($max_page > 1 || $always_show) {
			echo "<div class='page-nav'><div class='page-nav-intro'>Page $paged of $max_page</div>";
			if ($paged >= ($custom_range+2)) {
				echo '<div class="page-number"><a href="'.get_pagenum_link(1).'">First</a></div><div class="ellipses">…</div>';
			}
			if ($paged >= 2) {
				echo '<div class="page-number"><a href="'.get_pagenum_link($paged-1).'">?</a></div>';
			}
			for($i = $paged - $custom_range; $i <= $paged + $custom_range; $i++) {
				if ($i >= 1 && $i <= $max_page) {
					if($i == $paged) {
						echo "<div class='current-page-number'>$i</div>";
					}
					else {
						echo '<div class="page-number"><a href="'.get_pagenum_link($i).'">'.$i.'</a></div>';
					}
				}
			}
			if ($paged <= ($max_page-1)) {
				echo '<div class="page-number"><a href="'.get_pagenum_link($paged+1).'">?</a></div>';
			}
			if (($paged+$custom_range) < ($max_page)) {
				echo '<div class="ellipses">…</div><div class="page-number"><a href="'.get_pagenum_link($max_page).'">Last</a></div>';
			}
			echo "</div>";
		}
	}
}

remove_action('thesis_hook_after_content', 'thesis_post_navigation');
add_action('thesis_hook_after_content', 'numbered_page_nav');

I have it displaying relatively few page numbers at a time. To make it display more, modify this variable, which appears at the beginning of the custom_functions.php code: $pages_to_show = 4.

Please report back with any modifications or improvements!