如何从分页功能中删除prev / next按钮?

时间:2013-08-06 16:41:39

标签: wordpress function pagination

我正在使用Wordpress中的页面导航自定义功能,基于Bones主题(http://themble.com/bones)中使用的功能。该功能输出编号的页面链接,但也会输出上一页和下一页按钮,即使您在第一页或最后一页上也是如此。我想让它适应只在你不在第一页时显示上一页按钮,并且只在你不在最后一页时显示下一页按钮。希望这是有道理的!

这是功能:

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."";

}

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

为什么不离开呢? Bones official Git也很有帮助,在这个问题上打开一个支持问题。无论如何,尝试这个未经测试的代码

改变这个:

if ($end_page < $max_page) {
    $last_page_text = __( "Last", 'bonestheme' );
    echo '<li class="bpn-last-page-link"><a href="'.get_pagenum_link($max_page).'" title="'.$last_page_text.'">'.$last_page_text.'</a></li>';
}

对此:

if ($end_page < $max_page && != $max_page) {
    $last_page_text = __( "Last", 'bonestheme' );
    echo '<li class="bpn-last-page-link"><a href="'.get_pagenum_link($max_page).'" title="'.$last_page_text.'">'.$last_page_text.'</a></li>';
    }