有限的Wordpress自定义分页

时间:2016-01-01 16:58:42

标签: php wordpress pagination

大家好,我有一个为博客文章创建数字分页的功能:

function fb_paging_bar( $args = array() ) {

    $defaults = array(
        'range'           => 4,
        'custom_query'    => FALSE,
        'previous_string' => __( 'PREVIOUS', 'themename' ),
        'next_string'     => __( 'NEXT PAGE', 'themename' ),
        'view_fp'         => TRUE,
        'view_lp'         => TRUE,
        'before_output'   => '<div class="paginator" id="movie_paginator">',
        'after_output'    => '</div>'
    );

    $args = wp_parse_args( 
        $args, 
        apply_filters( 'fb_paging_bar_defaults', $defaults )
    );

    $args['range'] = (int) $args['range'] - 1;
    if ( !$args['custom_query'] )
        $args['custom_query'] = @$GLOBALS['wp_query'];
    $count = (int) $args['custom_query']->max_num_pages;
    $page  = intval( get_query_var( 'paged' ) );
    $ceil  = ceil( $args['range'] / 2 );

    if ( $count <= 1 )
        return FALSE;

    if ( !$page )
        $page = 1;

    if ( $count > $args['range'] ) {
        if ( $page <= $args['range'] ) {
            $min = 1;
            $max = $args['range'] + 1;
        } elseif ( $page >= ($count - $ceil) ) {
            $min = $count - $args['range'];
            $max = $count;
        } elseif ( $page >= $args['range'] && $page < ($count - $ceil) ) {
            $min = $page - $ceil;
            $max = $page + $ceil;
        }
    } else {
        $min = 1;
        $max = $count;
    }

    $echo = '';
    $previous = intval($page) - 1;
    $previous = esc_attr( get_pagenum_link($previous) );
    if ( $previous && (1 != $page) )
        $echo .= '<a class="page page-prev" href="' . $previous . '" title="' . __( 'PREVIOUS', 'themename' ) . '">' . $args['previous_string'] . '</a>';
    $firstpage = esc_attr( get_pagenum_link(1) );

    if ( $args['view_fp'] && $firstpage && (1 != $page) )
        $echo .= '<a class="page" href="' . $firstpage . '">' . __( '1', 'themename' ) . '</a>';

    if ( !empty($min) && !empty($max) ) {
        for( $i = $min; $i <= $max; $i++ ) {
            if ($page == $i) {
                $echo .= '<span class="page page-mobile disabled">' . str_pad( (int)$i, 1, '0', STR_PAD_LEFT ) . '</span>';
            } else {
                $echo .= sprintf( '<a href="%s">%002d</a>', esc_attr( get_pagenum_link($i) ), $i );
            }
        }
    }

    if ($args['view_lp']) {
        $lastpage = esc_attr( get_pagenum_link($count) );
        if ( $lastpage && ($count != $page) ) {
            $count = str_pad( (int)$count, 2, '0', STR_PAD_LEFT );
            $echo .= '<span class="page no-page page-mobile disabled">...</span><a class="page" href="' . $lastpage . '">' . $count . '</a>';
        }
    }

    $next = intval($page) + 1;
    $next = esc_attr( get_pagenum_link($next) );
    if ($next && ($count != $page) )
        $echo .= '<a class="page page-next" href="' . $next . '" title="' . __( 'NEXT PAGE', 'themename') . '">' . $args['next_string'] . '</a>';

    if ( isset($echo) )
        echo $args['before_output'] . $echo . $args['after_output'];
}

上面这段代码的结果就像第一张图片

enter image description here

但是我试图找到添加更多数字的正确方法因为你看到这个只显示第一个,当前和最后一个数字

有没有办法让它像第二个截图那样有更多数字?

enter image description here

0 个答案:

没有答案