public
Last active

WordPress pretty Pagination with first/last, next/prev and range parameters above/below current page

  • Download Gist
pagination.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
<?php
/*
Plugin Name: Easy Pagination Deamon
Plugin URI: http://wordpress.org/extend/plugins/stats/
Description: Offers the deamon_pagination($range) template tag for a sematically correct pagination.
Author: Franz Josef Kaiser
Author URI: http://say-hello-code.com
Version: 0.1
License: GPL v2 - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
Text Domain: pagination_deamon_lang
 
Copyright 20010-2011 by Franz Josef Kaiser
 
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
 
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
 
// Secure: don't load this file directly
if( !class_exists('WP') ) :
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
endif;
 
!defined('PAGE_LANG') ? define( 'PAGE_LANG', 'pagination_deamon_lang' ) : wp_die('The constant PAGE_LANG is already defined.');
 
!defined('PAGE_VERSION') ? define( 'PAGE_VERSION', 0.1 ) : wp_die('The constant PAGE_VERSION is already defined.');
 
/**
* Wordpress pagination for archives/search/etc.
*
* 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', PAGE_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', PAGE_LANG).'</a></li>';
echo "\n".'</ul>'."\n";
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.