自定义页面中用户的分页

时间:2013-04-11 04:17:39

标签: wordpress pagination

我有一个显示所有用户的页面。用户类型是作者。显示所有代理。

                    $args = array(
                    'role' => 's2member_level2', 
                        'meta_query' => array(
                            array(
                                'key' => $filter,
                                'value' => $_GET['filter'],
                                'compare' => 'LIKE'
                            )
                        )
                    );
                }
                  $users = get_users($args);
                  if ($users) {
                    foreach ($users as $user) {
                      $user_profile_data  = get_user_meta($user->ID,'wp_s2member_custom_fields',true);
                      //print_r($user_profile_data);
                      echo '<div class="user_data" >';
                      echo '<a href="'.site_url().'/author/'.$user->user_login.'">'.user_avatar_get_avatar($user_info->ID,'110').'</a>';
                      echo '<div class="user-info">';
                      //print_r($users);
                      echo '<h6>'.'<a href="'.site_url().'/author/'.$user->user_login.'">'.$user->display_name.'</a></h6>';
                      if ($user_profile_data[contact_number])
                      echo '<p> call-'.$user_profile_data[contact_number].'</p>';
                      else
                      echo '<p> Phone number not specified.</p>';
                      echo '<p>'.$user->user_email.'</p>';
                      echo '</div>';
                      echo '<div class="agent_specify" >';
                      $agent_specify = $user_profile_data[posting_preferance];
                      echo '<p>Agent Speciality :</p>';
                      if ($agent_specify) {
                      echo '<ul>';
                      foreach ($agent_specify as $specification){
                          echo '<li>'.$specification.'</li>';
                      }
                      echo '</ul>';
                      }
                      else {
                      echo '<p style="font-weight: normal;">Not specified</p>';
                      }
                      echo '</div>';
                      echo '<a href="'.site_url().'/author/'.$user->user_login.'/#contact">contact &raquo;</a>';
                      echo '</div>';
                    }
                  }
                  else {
                    echo '<h4>No agents found.</h4>';
                  }
              ?>
            </div>

我可以找到用户列表,但我想在这里分页,因为我在页面中有很多用户。

1 个答案:

答案 0 :(得分:5)

好的,你可以使用WP_User_Query()函数进行这个juss check out,我为你做了一些工作。

    $no=5;// 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;
}
    $args = array(
                        'role' => 's2member_level2',
        'number' => $no, 'offset' => $offset,
                            'meta_query' => array(
                                array(
                                    'key' => $filter,
                                    'value' => $_GET['filter'],
                                    'compare' => 'LIKE'
                                )
                            ));

    $user_query = new WP_User_Query( $args );
    if ( !empty( $user_query->results ) ) {
        foreach ( $user_query->results as $user ) {
               ..... ur code goes here
        }
    }
else {
 echo '<h4>No agents found.</h4>';
}

对于分页,它取决于你的结构如果它的永久链接是名称然后你可以使用分页 代码为

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

希望这对你有用:)