Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress WP_用户_查询分页_Wordpress_Pagination - Fatal编程技术网

Wordpress WP_用户_查询分页

Wordpress WP_用户_查询分页,wordpress,pagination,Wordpress,Pagination,我有一个简短的代码片段,可以根据元数据查询网站用户。代码已经很好地工作了。现在,我唯一的问题是,我似乎不知道如何使分页工作,就像在WorpAddress帖子上使用分页一样。你的帮助对我们意义重大。提前谢谢你,给stackoverflow更多的权力 以下是需要分页的代码: <ul id="ulfriends"> <?php // The User Query $user_query = new WP_User_Query( $args = array ( 'number' =&

我有一个简短的代码片段,可以根据元数据查询网站用户。代码已经很好地工作了。现在,我唯一的问题是,我似乎不知道如何使分页工作,就像在WorpAddress帖子上使用分页一样。你的帮助对我们意义重大。提前谢谢你,给stackoverflow更多的权力 以下是需要分页的代码:

<ul id="ulfriends">
<?php 
// The User Query
$user_query = new WP_User_Query( $args = array (
'number' => 10,
'meta_query'     => array(
    array(
    'key'       => 'sponsor',
    'value'     => $current_user->user_nicename,
    'compare'   => '=',
    'type'      => 'CHAR',
    ),
    ),
    'count_total'    => true,
) );
echo count($user_query->results);
// User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
    echo '<li>' . $user->first_name . ' ' . $user->last_name . '<br>' . $user->date_activation . '<br>' . get_avatar( $user->user_email, 165 ) . '</li>' ;
    }
} else {
    echo 'No users found.';

} 
?>
</ul>
试试这段代码

<ul id="ulfriends">
 <?php 

 $no=12;// total no of author to display

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    if($paged==1){
      $offset=0;  
    }else {
       $offset= ($paged-1)*$no;
    }


 $user_query = new WP_User_Query( array( 'role' => 'Subscriber',  'number' => $no, 'offset' => $offset ) );
       if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<li>' . $user->first_name . ' ' . $user->last_name . '<br>' . $user->date_activation . '<br>' . get_avatar( $user->user_email, 165 ) . '</li>' ;
    }
        } else {
            echo 'No users found.';
        } 
 ?>           
</ul>
<?php
            $total_user = $user_query->total_users;  
            $total_pages=ceil($total_user/$no);

              echo paginate_links(array(  
                  'base' => get_pagenum_link(1) . '%_%',  
                  'format' => '?paged=%#%',  
                  'current' => $paged,  
                  'total' => $total_pages,  
                  'prev_text' => 'Previous',  
                  'next_text' => 'Next',
                  'type'     => 'list',
                )); 
?>

Sandeep–我对你的爱比一个能举起卡车的人更强烈,你救了我!附议。对于自定义分页情况(用户查询),这是一个很好的解决方案。