Wordpress WP_User_Query分页

时间:2014-05-05 14:54:30

标签: wordpress pagination

我有一小段代码,可根据元数据查询网站用户。代码已经完美无缺。现在,我唯一的问题是,我似乎无法弄清楚如何使分页工作就像你在worpdress帖子上使用分页一样。你的帮助对我们意义重大。提前感谢您对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>

1 个答案:

答案 0 :(得分:6)

试试这段代码

<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',
                )); 
?>