我一直很难使用自定义插件为我的wordpress网站后端的用户列表进行分页。它实际上显示链接的所有内容,但是当我点击"下一页"链接它加载并保持相同而不是移动到第2页。这是我的代码(我从stackoverflow上的另一个答案得到了这个但是没有成功将分页链接增加到下一页):
function oragatesoft_expert(){
// Pagination vars
$current_page = get_query_var('paged') ? (int) get_query_var('paged') : 1;
echo $current_page;
$users_per_page = 2; // RAISE THIS AFTER TESTING ;)
$args = array(
'number' => $users_per_page, // How many per page
'paged' => $current_page // What page to get, starting from 1.
);
$users = new WP_User_Query( $args );
$total_users = $users->get_total(); // How many users we have in total (beyond the current page)
$num_pages = ceil($total_users / $users_per_page); // How many pages of users we will need
?>
<h3>Page <?php echo $current_page; ?> of <?php echo $num_pages; ?></h3>
<p>Displaying <?php echo $users_per_page; ?> of <?php echo $total_users; ?> users</p>
<table>
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php
if ( $users->get_results() ) foreach( $users->get_results() as $user ) {
$firstname = $user->first_name;
$lastname = $user->last_name;
$email = $user->user_email;
?>
<tr>
<td><?php echo esc_html($firstname); ?></td>
<td><?php echo esc_html($lastname); ?></td>
<td><?php echo esc_html($email); ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<p>
<?php
// Previous page
if ( $current_page > 1 ) {
echo '<a href="'. add_query_arg(array('paged' => $current_page-1)) .'">Previous Page</a>';
}
// Next page
if ( $current_page < $num_pages ) {
echo '<a href="'. add_query_arg(array('paged' => $current_page+1)) .'">Next Page</a>';
}
?>
</p>
<?php
}
答案 0 :(得分:0)
我可以确认,使用类似方法对遇到此问题的其他任何人都有效。
$args = array (
'number' => 1, // use this like you would posts_per_page
'paged' => 2,
);
$wp_user_query = new \WP_User_Query($args);
// Get the results
$users = $wp_user_query->get_results();