Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created February 9, 2011 13:23
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save franz-josef-kaiser/818457 to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/818457 to your computer and use it in GitHub Desktop.
WordPress pretty Pagination with first/last, next/prev and range parameters above/below current page
<?php
/**
* Wordpress pagination for archives/search/etc.
*
* @author: Franz Josef Kaiser [http://say-hello-code.com]
*
* Semantically correct pagination inside an unordered list
*
* Displays: First « 1 2 3 4 » Last
* First/Last only appears if not on first/last page
* Shows next/previous links «/»
* Accepts a range attribute (default = 5) to adjust the number
* of direct page links that link to the pages above/below the current one.
*
* @param (int) $range
*/
function oxo_pagination( $range = 5 ) {
// $paged - number of the current page
global $paged, $wp_query;
// How much pages do we have?
if ( !$max_page )
$max_page = $wp_query->max_num_pages;
// We need the pagination only if there is more than 1 page
if ( $max_page > 1 )
if ( !$paged ) $paged = 1;
echo "\n".'<ul>'."\n";
// On the first page, don't put the First page link
if ( $paged != 1 )
echo '<li><a href='.get_pagenum_link(1).'>'.__('First', CHILD_LANG).' </a></li>';
// To the previous page
echo '<li>';
previous_posts_link(' &laquo; '); // «
echo '</li>';
// We need the sliding effect only if there are more pages than is the sliding range
if ( $max_page > $range ) :
// When closer to the beginning
if ( $paged < $range ) :
for ( $i = 1; $i <= ($range + 1); $i++ ) {
$class = $i == $paged ? 'current' : '';
echo '<li><a href="'.get_pagenum_link($i).'" class="paged-num '.$class.'"> '.$i.' </a></li>';
}
// When closer to the end
elseif ( $paged >= ( $max_page - ceil($range/2)) ) :
for ( $i = $max_page - $range; $i <= $max_page; $i++ ){
$class = $i == $paged ? 'current' : '';
echo '<li><a href="'.get_pagenum_link($i).'" class="paged-num '.$class.'"> '.$i.' </a></li>';
}
endif;
// Somewhere in the middle
elseif ( $paged >= $range && $paged < ( $max_page - ceil($range/2)) ) :
for ( $i = ($paged - ceil($range/2)); $i <= ($paged + ceil($range/2)); $i++ ) {
$class = $i == $paged ? 'current' : '';
echo '<li><a href="'.get_pagenum_link($i).'" class="paged-num '.$class.'"> '.$i.' </a></li>';
}
// Less pages than the range, no sliding effect needed
else :
for ( $i = 1; $i <= $max_page; $i++ ) {
$class = $i == $paged ? 'current' : '';
echo '<li><a href="'.get_pagenum_link($i).'" class="paged-num '.$class.'"> '.$i.' </a></li>';
}
endif;
// Next page
echo '<li>';
next_posts_link(' &raquo; '); // »
echo '</li>';
// On the last page, don't put the Last page link
if ( $paged != $max_page )
echo '<li><a href='.get_pagenum_link($max_page).'> '.__('Last', CHILD_LANG).'</a></li>';
echo "\n".'</ul>'."\n";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment