Skip to content

Instantly share code, notes, and snippets.

@cjmox
Created February 28, 2014 22:23
Show Gist options
  • Save cjmox/9281258 to your computer and use it in GitHub Desktop.
Save cjmox/9281258 to your computer and use it in GitHub Desktop.
Custom WordPress Pagination
// Modified code from http://design.sparklette.net/teaches/how-to-add-wordpress-pagination-without-a-plugin/
// Could be a lot cooler if someone wants to write a way to add in an ellipsis
// after a range of pages has been displayed,
// similar to how wp_paginate_links works
function sf_pagination($pages = '', $range = 4) {
$showitems = ($range * 2)+1;
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages)
{
$pages = 1;
}
}
if(1 != $pages) {
echo "<div class=\"pagination\">";
// Shows which page you're out of the total number of pages
echo "<span>Page ".$paged." of ".$pages."</span>";
echo "<div class=\"previous-page\">";
// Shows link to First page of posts, disabled if you're already on the first page
if($paged < 2) echo "<span class='disabled'>&laquo; First</span>";
if($paged > 1) echo "<a href='".get_pagenum_link(1)."'>&laquo; First</a>";
// Shows link to Previous page of posts, disabled if you're already on the first page
if($paged < 2) echo "<span class='disabled'>&lsaquo; Previous</span>";
if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>&lsaquo; Previous</a>";
echo "</div>"; // End previous page
echo "<div class=\"numbered\">";
for ($i=1; $i <= $pages; $i++) {
if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"<a href='".get_pagenum_link($i)."' class=\"inactive\">".$i."</a>";
}
}
echo "</div>"; // End numbered
echo "<div class=\"next-page\">";
if ($paged < $pages && $showitems < $pages) echo "<a href=\"".get_pagenum_link($paged + 1)."\">Next &rsaquo;</a>";
if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Last &raquo;</a>";
echo "</div>"; // End next page
echo "</div>\n"; // End pagination
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment