在分页中包括前导零

时间:2020-07-17 作者:Auromnia

我是WordPress开发的新手,在过去几个月里一直在自学如何从头开始开发主题。通常我可以通过论坛自己解决问题,但我在这方面找不到太多。

如果数字小于10,我一直试图在分页中添加前导零。

一、 电子邮件:<;更新01 02 03。。。10岁以上>;

这是我的分页代码。任何指向正确方向的指针都将不胜感激!

if ( !function_exists(\'palfrey_pagination\') ) {
function palfrey_pagination( $range = 5 ) {

    if( is_singular() )
        return;

    // $paged - number of the current page
    global $paged, $wp_query;

    // Stop execution if there\'s only 1 page.
    if( $wp_query->max_num_pages <= 1 )
        return;
 
    $paged = get_query_var( \'paged\' ) ? absint( get_query_var( \'paged\' ) ) : 1;

    if ( !$max_page )
        $max   = intval( $wp_query->max_num_pages );

    if ( $max_page > 1 )
            if ( !$paged ) $paged = 1;
 
    // Add current page to the array.
    if ( $paged >= 1 )
        $links[] = $paged;
 
    // Add the pages around the current page to the array.
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }
 
    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }
    
    // The pagination
    echo "\\n" . \'<div class="content-block-common-large">
                    <div class="block-wrap">
                        <div class="inline-group flex-group relative align-center column-align-bottom">
                            <div class="column responsive width-450">
                                <ul class="pagination display-block relative align-center width-1of1">\' . "\\n";
 
    // Link to \'Newer\' posts.
    if ( get_previous_posts_link() ) {
        printf( \'<li>%s</li>\' . "\\n", get_previous_posts_link( \'<div class="display-inline-block relative float-left"><span class="pagination-prev">Newer</span></div>\' ) );
    }else{
    echo \'<li><div class="display-inline-block relative float-left pointer-events-none" style="opacity: .6;"><span class="pagination-prev">Newer</span></div></li>\';
    }
    
    // Link to first page, plus ellipses if necessary.
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? \' class="active"\' : \'\';
 
        printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( 1 ) ), \'1\' );
 
        if ( ! in_array( 2, $links ) )
            echo \'<li>…</li>\';
    }
 
    // Link to current page, plus 2 pages in either direction if necessary.
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? \' class="active"\' : \'\';
        printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $link) ), $link );
    }
 
    // Link to last page, plus ellipses if necessary.
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo \'<li>…</li>\' . "\\n";
 
        $class = $paged == $max ? \' class="active"\' : \'\';
        printf( \'<li%s><a href="%s">%s</a></li>\' . "\\n", $class, esc_url( get_pagenum_link( $max) ), $max );
    }
 
    // Link to \'Older\' posts.
    if ( get_next_posts_link() ) {
        printf( \'<li>%s</li>\' . "\\n", get_next_posts_link( \'<div class="display-inline-block relative float-right"><span class="pagination-next">Older</span></div>\' ) );
    }else{
    echo \'<li><div class="display-inline-block relative float-right pointer-events-none" style="opacity: .6;"><span class="pagination-next">Older</span></div></li>\';
    }
 
    echo "\\n" . \'           </ul>
                        </div>
                    </div>
                </div>
            </div>\' . "\\n";
 
    }
}

1 个回复
SO网友:Rick Hellewell

当您的代码要输出一个数字时,请调整数字($pagenumber 在本代码示例中)包含前导空格

$pagenumber = 123;

echo sprintf("%\'.09d\\n", $pagenumber);
将输出

000000123
参见手册:https://www.php.net/manual/en/function.sprintf.php , 示例2和其中的其他示例。