WP_USER_QUERY的带数字分页

时间:2020-07-28 作者:user8463989

我对下一个/上一个链接进行了分页,但我也想显示数字,以便用户可以单击2、3、4等。对于WP\\U user\\U查询,分页似乎更复杂,因为据我所知,没有任何默认的WordPress分页。就我所知,以下内容对于下一个和上一个链接是正确的。

$current_page = get_query_var(\'paged\') ? (int) get_query_var(\'paged\') : 1;
$users_per_page = 2;

$args = array(
    \'number\' => $users_per_page, 
    \'paged\' => $current_page
);

$wp_user_query = new WP_User_Query( $args );

$total_users = $wp_user_query->get_total();
$num_pages = ceil($total_users / $users_per_page);

       <?php
        // Previous page
        if ( $current_page > 1 ) {
            echo \'<a href="\'. add_query_arg(array(\'paged\' => $current_page-1)) .\'" class="prev">Prev</a>\';
        }

        // Next page
        if ( $current_page < $num_pages ) {
            echo \'<a href="\'. add_query_arg(array(\'paged\' => $current_page+1)) .\'" class="next">Next</a>\';
        }
        ?>

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

You can use paginate_links():

echo paginate_links( array(
  \'current\' => $current_page,
  \'total\'   => $num_pages,
) );
SO网友:maulik zwt

请尝试以下函数进行数字分页,并根据您的要求更改查询变量。

function wpbeginner_numeric_posts_nav() {

    if( is_singular() )
        return;

    global $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;
    $max   = intval( $wp_query->max_num_pages );

    /** 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;
    }

    echo \'<div class="navigation"><ul>\' . "\\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( \'<li>%s</li>\' . "\\n", get_previous_posts_link() );

    /** 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 );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( \'<li>%s</li>\' . "\\n", get_next_posts_link() );

    echo \'</ul></div>\' . "\\n";

}

相关推荐

Adding pagination to my theme

我想为我的wordpress主题添加一个分页。然而,对我来说,这并不容易,因为我的主题是从插件“AT Posts Column”构建的,该插件在主页上显示最新的帖子。我想将分页添加到此插件:)。我尝试插入分页插件,并将代码粘贴到下面的文件的末尾。确实出现了分页,但当我进入下一页时,只有www.website上的链接发生了变化。com/page/2。请帮助:)。我的网站是http://consolezone.pl插件文件“在Posts列中”:<?php /** * Custom co