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

时间:2014-04-07 06:57:58

标签: php wordpress pagination

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

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();
}

现在该怎么办

2 个答案:

答案 0 :(得分:1)

在我的页面模板中,我使用

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'
     ));
}
}

答案 1 :(得分:0)

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 ;