Created
May 29, 2011 15:32
-
-
Save roose/997863 to your computer and use it in GitHub Desktop.
WordPress pagination, based on WP-Pagenavi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
### Function: Page Navigation | |
function wp_pagination($before = '', $after = '') { | |
global $wpdb, $wp_query; | |
if (!is_single()) { | |
$request = $wp_query->request; | |
$posts_per_page = intval(get_query_var('posts_per_page')); | |
$paged = intval(get_query_var('paged')); | |
$numposts = $wp_query->found_posts; | |
$max_page = $wp_query->max_num_pages; | |
if(empty($paged) || $paged == 0) { | |
$paged = 1; | |
} | |
$always_show = 1; | |
$pages_to_show = 5; | |
$pages_to_show_minus_1 = $pages_to_show - 1; | |
$half_page_start = floor($pages_to_show_minus_1/2); | |
$half_page_end = ceil($pages_to_show_minus_1/2); | |
$start_page = $paged - $half_page_start; | |
if($start_page <= 0) { | |
$start_page = 1; | |
} | |
$end_page = $paged + $half_page_end; | |
if(($end_page - $start_page) != $pages_to_show_minus_1) { | |
$end_page = $start_page + $pages_to_show_minus_1; | |
} | |
if($end_page > $max_page) { | |
$start_page = $max_page - $pages_to_show_minus_1; | |
$end_page = $max_page; | |
} | |
if($start_page <= 0) { | |
$start_page = 1; | |
} | |
if($max_page > 1 || $always_show == 1) { | |
$pages_text = 'Page '.$paged.' of '.$max_page; | |
echo $before.'<ul class="pagination">'."\n"; | |
if(!empty($pages_text)) { | |
echo '<li class="pages"><span>'.$pages_text.'</span></li>'; | |
} | |
if ($start_page >= 2 && $pages_to_show < $max_page) { | |
echo '<li class="first"><a href="'.clean_url(get_pagenum_link()).'" title="« First">« First</a></li>'; | |
echo '<li class="extend"><span>...</span></li>'; | |
} | |
if(get_previous_posts_link()) echo '<li class="prev">'.get_previous_posts_link('«').'</li>'; | |
for($i = $start_page; $i <= $end_page; $i++) { | |
if($i == $paged) { | |
echo '<li class="current"><span>'.$i.'</span></li>'; | |
} else { | |
echo '<li class="page"><a href="'.clean_url(get_pagenum_link($i)).'" title="'.$i.'">'.$i.'</a></li>'; | |
} | |
} | |
if(get_next_posts_link()) echo '<li class="next">'.get_next_posts_link('»').'</li>'; | |
if ($end_page < $max_page) { | |
echo '<li class="extend"><span>...</span></li>'; | |
echo '<li class="last"><a href="'.clean_url(get_pagenum_link($max_page)).'" title="Last »">Last »</a></li>'; | |
} | |
echo '</ul>'.$after."\n"; | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment