如何在WordPress中以数字显示分页


How to show pagination in WordPress in numbers

我写了一个WordPress分页功能,这个函数工作得很好,但这个函数显示所有页码,但我想喜欢这个( 例如: « 上一页 1 ...3 4 5 ...9 下一页 » )但我的功能显示(例如:<<上一页 1 2 3 4 5 6 7 8 9 下一页>> )

function tender_paginate()
{
    global $paged, $wp_query;
    $curpage = $paged ? $paged : 1;
    $total = $wp_query->max_num_pages;
    $args = array(
        'posts_per_page' => $total,
        'paged' => $paged,
        'post_type' => 'post',
        'category_name' => 'Procurement Tender',
    );
    $query = new WP_Query($args);
    echo '<ul class="page-numbers">';
    echo '<li><a class="first page button" href="'.get_pagenum_link(1).'">&laquo; First</a></li>';
    echo '<li><a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">&lsaquo; Previous</a></li>';
    for( $i=1; $i<=$query->max_num_pages; $i++ )
    {
        $n_display = number_format_i18n($i);
        echo '<li><a class="'.($active = $i == $curpage ? 'page-current ' : '').'page button" href="'.get_pagenum_link($i).'">'.$n_display.'</a></li>';
    }
    echo '<li><a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $query->max_num_pages ? $curpage+1 : $query->max_num_pages)).'">Next &rsaquo;</a></li>';
    echo '<li><a class="last page button" href="'.get_pagenum_link($query->max_num_pages).'">Last &raquo;</a></li>';
    echo '</ul>';
    wp_reset_postdata();
}

现在我该怎么办

在我的页面模板中使用

global $paged
query_posts('post_type=post&category_name=Procurement Tender&meta_key=sah_post_setting&order=DESC&paged='.$paged);

如果您想在模板页面上显示分页,请不要忘记在query_posts中添加 paged=$paged

和功能是

function sah_paginate()
{
global $wp_query;
$total = $wp_query->max_num_pages;
// only bother with the rest if we have more than 1 page!
if ( $total > 1 )  {
     // get the current page
     if ( !$current_page = get_query_var('paged') )
          $current_page = 1;
     // structure of "format" depends on whether we're using pretty permalinks
     if( get_option('permalink_structure') ) {
         $format = '?paged=%#%';
     } else {
         $format = 'page/%#%/';
     }
     echo paginate_links(array(
          'base'     => get_pagenum_link(1) . '%_%',
          'format'   => $format,
          'current'  => $current_page,
          'total'    => $total,
          'mid_size' => 4,
          'type'     => 'list'
     ));
}
}
function paging_nav() {

$paged        = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args   = array();
$url_parts    = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
    wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format  = $GLOBALS['wp_rewrite']->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $GLOBALS['wp_rewrite']->using_permalinks() ? user_trailingslashit( 'page/%#%', 'paged' ) : '?paged=%#%';
$links = paginate_links( array(
    'base'     => $pagenum_link,
    'format'   => $format,
    'total'    => $GLOBALS['wp_query']->max_num_pages,
    'current'  => $paged,
    'mid_size' => 1,
    'add_args' => array_map( 'urlencode', $query_args ),
    'prev_text' => __( 'Previous Page ' ),
    'next_text' => __( 'Next Page' ),
) );
    echo $links ;