Pagination for user list

时间:2013-08-26 作者:user2311322

我使用get_user() 作用但我需要把结果分页。我试了很多,但没有成功。下面是代码,它没有按预期工作:

$args = array( \'meta_query\' => array(
    array(
        \'key\'   => \'ib_s2member_custom_fields\',
        \'value\' => trim($_GET["country"]),
        \'compare\' => \'LIKE\'
    ),
array(
        \'key\'   => \'ib_s2member_custom_fields\',
        \'value\' => trim($_GET["city"]),
        \'compare\' => \'LIKE\'
    ),
    array(
        \'key\'   => \'ib_s2member_custom_fields\',
        \'value\' => $first_name,
        \'compare\' => \'LIKE\'
    ),
array(
        \'key\'   => \'ib_s2member_custom_fields\',
        \'value\' => $last_name,
        \'compare\' => \'LIKE\'
    )
) );

$users = get_users( $args );

1 个回复
SO网友:Vinod Dalvi

您可以使用WP_User_Query 类而不是get\\u users()函数来实现这一点。新修改的代码将类似于以下代码。

<?php
/*
 * We start by doing a query to retrieve all users
 * We need a total user count so that we can calculate how many pages there are
 */

$count_args  = array(
\'number\'    => 999999      
);
$user_count_query = new WP_User_Query($count_args);
$user_count = $user_count_query->get_results();

// count the number of users found in the query
$total_users = $user_count ? count($user_count) : 1;

// grab the current page number and set to 1 if no page number is set
$page = isset($_GET[\'p\']) ? $_GET[\'p\'] : 1;

// how many users to show per page
$users_per_page = 5;

// calculate the total number of pages.
$total_pages = 1;
$offset = $users_per_page * ($page - 1);
$total_pages = ceil($total_users / $users_per_page);

// main user query
$args = array(
\'number\'    => $users_per_page,
\'offset\'    => $offset, 
\'meta_query\' => array(
\'relation\' => \'OR\',
array(
    \'key\'     => \'country\',
    \'value\'   => \'Israel\',
    \'compare\' => \'=\'
),
array(
    \'key\'     => \'age\',
    \'value\'   => array( 20, 30 ),
    \'type\'    => \'numeric\',
    \'compare\' => \'BETWEEN\'
)
)
);

$user_query = new WP_User_Query( $args );

// Get the results
$users = $user_query->get_results();

// User Loop
if ( ! empty( $users->results ) ) {
foreach ( $users->results as $user ) {
echo \'<p>\' . $user->display_name . \'</p>\';
}
} else {
echo \'No users found.\';
}

// grab the current query parameters
$query_string = $_SERVER[\'QUERY_STRING\'];

// The $base variable stores the complete URL to our page, including the current page arg

// if in the admin, your base should be the admin URL + your page
$base = admin_url(\'your-page-path\') . \'?\' . remove_query_arg(\'p\', $query_string) . \'%_%\';

// if on the front end, your base is the current page
//$base = get_permalink( get_the_ID() ) . \'?\' . remove_query_arg(\'p\', $query_string) . \'%_%\';

echo paginate_links( array(
\'base\' => $base, // the base URL, including query arg
\'format\' => \'&p=%#%\', // this defines the query parameter that will be used, in this case "p"
\'prev_text\' => __(\'&laquo; Previous\'), // text for previous page
\'next_text\' => __(\'Next &raquo;\'), // text for next page
\'total\' => $total_pages, // the total number of pages we have
\'current\' => $page, // the current page
\'end_size\' => 1,
\'mid_size\' => 5,
));
?>
有关WP\\u User\\u Query的更多信息,请访问this page.

结束

相关推荐

Pagination in category

Updated: I have 50 categories and i wants to show each 5 category on one page and next 5 on next page and next 5 on next page. and so on. with pagination.我在WordPress网站上有50个类别。我想在每一页上显示五个类别,用10篇文章分页。我已经尝试过了,但分页并没有产生效果。<?php // get all the categ