Tuesday, 6 August 2013

How to remove prev/next buttons from pagination function?

How to remove prev/next buttons from pagination function?

I'm working with a custom function for page navigation in Wordpress, based
on the one used in the Bones theme (http://themble.com/bones). The
function outputs numbered page links, but also outputs both previous and
next page buttons, even when you are on the first or last page. I want to
adapt it to only show the previous page button when you are not on the
first page, and only show the next page button when you are not on the
last page. Hopefully that makes sense!
Here's the function:
function custom_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 ( $numposts <= $posts_per_page ) { return; }
if(empty($paged) || $paged == 0) {
$paged = 1;
}
$pages_to_show = 7;
$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.'<nav class="pagination"><ol class="custom_page_navi">'."";
if ($start_page >= 2 && $pages_to_show < $max_page) {
$first_page_text = __( "First");
echo '<li class="cpn-first-page-link"><a href="'.get_pagenum_link().'"
title="'.$first_page_text.'">'.$first_page_text.'</a></li>';
}
echo '<li class="cpn-prev-link">';
previous_posts_link('');
echo '</li>';
for($i = $start_page; $i <= $end_page; $i++) {
if($i == $paged) {
echo '<li class="cpn-current">'.$i.'</li>';
} else {
echo '<li><a href="'.get_pagenum_link($i).'">'.$i.'</a></li>';
}
}
echo '<li class="cpn-next-link">';
next_posts_link('');
echo '</li>';
if ($end_page < $max_page) {
$last_page_text = __( "Last");
echo '<li class="cpn-last-page-link"><a
href="'.get_pagenum_link($max_page).'"
title="'.$last_page_text.'">'.$last_page_text.'</a></li>';
}
echo '</ol></nav>'.$after."";
}
Any ideas?

No comments:

Post a Comment