<?php
#Page Navi
function page_navi($before = '', $after = '') {
global $wpdb, $wp_query;
$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;
}
$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;
}
echo $before.'<center><div class="page_navi">'."\n";
if ($start_page >= 2 && $pages_to_show < $max_page) {
$first_page_text = "First";
echo '<a href="'.get_pagenum_link().'" title="'.$first_page_text.'">'.$first_page_text.'</a>';
}
previous_posts_link('«');
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $paged) {
echo '<span class="current">'.$i.'</span>';
} else {
echo '<a href="'.get_pagenum_link($i).'">'.$i.'</a>';
}
}
next_posts_link('»');
if ($end_page < $max_page) {
$last_page_text = "Last";
echo '<a href="'.get_pagenum_link($max_page).'" title="'.$last_page_text.'">'.$last_page_text.'</a>';
}
echo '</div></center>'.$after."\n";
}
?>