Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/8695956 to your computer and use it in GitHub Desktop.
Save jchristopher/8695956 to your computer and use it in GitHub Desktop.
<?php
/* Numbered Pagination */
function numbered_page_nav($prelabel = '', $nxtlabel = '', $pages_to_show = 6, $always_show = false) {
global $wp_query, $request, $posts_per_page, $wpdb, $paged;
$custom_range = round($pages_to_show/2);
if (!is_single()) {
if(!is_category()) {
preg_match('#FROM\s(.*)\sORDER BY#siU', $request, $matches);
}
else {
preg_match('#FROM\s(.*)\sGROUP BY#siU', $request, $matches);
}
if( !is_search() ) {
$blog_post_count = $matches[1];
$numposts = $wpdb->get_var("SELECT COUNT(DISTINCT ID) FROM $blog_post_count");
$max_page = ceil($numposts /$posts_per_page);
} else {
// we're going to pull pagination straight from $wpdb
$blog_post_count = $wp_query->found_posts;
$max_page = $wp_query->max_num_pages;
}
if(empty($paged)) {
$paged = 1;
}
if($max_page > 1 || $always_show) {
echo "<div class='page-nav'>";
if ($paged >= ($pages_to_show-1)) {
echo '<a href="'.get_pagenum_link().'"><div class="page-number">1</div></a><div class="elipses">...</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 ' <a href="'.get_pagenum_link($i).'"><div class="page-number">'.$i.'</div></a> ';
}
}
}
if (($paged+$custom_range) < ($max_page)) {
echo ' <div class="elipses">...</div><a href="'.get_pagenum_link($max_page).'"> <div class="page-number">'.$max_page.'</div></a>';
}
echo "</div>";
}
}
}
add_action('hook_bottom_pagination_container', 'numbered_page_nav');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment