最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pagination - How to paginate the get_users function?

programmeradmin1浏览0评论

Im using the get_users function to show a custom list of users on the site. The only issue Im having a problem figuring out now is how to paginate the result.

This is a sample of the function that Im using:

There doesn't seem to be an obvious way of creating a pagination for the this function. I would appreciate some help with this.

Im using the get_users function to show a custom list of users on the site. The only issue Im having a problem figuring out now is how to paginate the result.

This is a sample of the function that Im using:

There doesn't seem to be an obvious way of creating a pagination for the this function. I would appreciate some help with this.

Share Improve this question asked Oct 18, 2011 at 19:18 user1893user1893 3591 gold badge9 silver badges22 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 9

First get the total number of users:

$total_users = count_users();
//var_dump($total_users); //for debugging purpose
$total_users = $total_users['total_users'];

and the current page:

$paged = get_query_var('paged');

Set a variable that will decide how many users to display per page:

$number = 20; // ie. 20 users page page 

Then, in your $args array add the offset if the current page != 0, and the max number of users to return:

'offset' => $paged ? ($paged - 1) * $number : 0,
'number' => $number,

Now add your code from above, and create the page links:

// display the user list here

if($total_users > $number){

  $pl_args = array(
     'base'     => add_query_arg('paged','%#%'),
     'format'   => '',
     'total'    => ceil($total_users / $number),
     'current'  => max(1, $paged),
  );

  // for ".../page/n"
  if($GLOBALS['wp_rewrite']->using_permalinks())
    $pl_args['base'] = user_trailingslashit(trailingslashit(get_pagenum_link(1)).'page/%#%/', 'paged');

  echo paginate_links($pl_args);
}

See paginate_links() for a full list of arguments....

发布评论

评论列表(0)

  1. 暂无评论