您可以使用paginate_links() 通过wordpress核心API,可以更轻松地为自定义查询分页。您不需要再进行一次查询来统计所有用户。
<?php
$users_per_page = 10; // total no of author to display
$paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;
if( (int)$paged === 1 ){
$offset = 0;
} else {
$offset = ($paged-1) * $users_per_page;
}
$args = array(
\'meta_key\' => \'location\',
\'number\' => $users_per_page,
\'offset\' => $offset,
\'meta_value\' => $flow_location,
\'meta_compare\' => \'=\',
\'role\' => \'admin\',
\'orderby\' => \'ID\',
\'order\' => \'DESC\'
);
$fields = array(\'fields\' => \'all_with_meta\');
$user_query_subs = new WP_User_Query( $args, $fields );
$users = $user_query_subs->get_results();
if ( !empty( $users ) ) {
foreach ( $users as $user ) {
// code goes here
}
}
else {
echo \'No flowers found.\';
}
?>
然后分页,记住这取决于你的结构
如果使用permalinks,则可以使用以下代码:
<?php
$query_string = $_SERVER[\'QUERY_STRING\']; // current query parameters
$total_users = $user_query_subs->total_users; // total number of users
$total_pages = ceil( $total_user/$users_per_page ); // total number of pages
// if on the front end, your base is the current page
$base = get_permalink( get_the_ID() ) . \'?\' . remove_query_arg(\'paged\', $query_string) . \'%_%\';
$paginate_args = array(
\'base\' => $base,
\'format\' => \'?paged=%#%\', // define the query parameter that will be used, in this case "paged"
\'current\' => $paged,
\'total\' => $total_pages,
\'prev_text\' => \'Previous\',
\'next_text\' => \'Next\' ,
\'end_size\' => 1,
\'mid_size\' => 5
);
echo paginate_links($paginate_args);
?>