考虑将get_users()
纳入ajax分页结果。并找到以下解决方案。
https://wordpress.stackexchange.com/questions/232513/pagination-using-ajax
有人可以帮忙解决,我怎样才能使用类似的东西来显示使用wordpress的get_users()
函数获得的结果。
目前有以下代码,
我想显示使用ajax加载下一页。
<h4>List of Agents</h4>
<table>
<tr>
<th>Agent ID</th>
<th>Email</th>
<th>Action</th>
</tr>
<?php
$number = 3; //max display per page
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1; //current number of page
$offset = ($paged - 1) * $number; //page offset
$users = get_users(array('role' => 'agent')); //get all the lists of users
$args = array(
'offset' => $offset,
'number' => $number,
'role' => 'agent',
'fields' => array( 'ID', 'user_login', 'user_email' )
);
$query = get_users($args);//query the maximum users that we will be displaying
$total_users = count($users);//count total users
$total_query = count($query);//count the maximum displayed users
$total_pages = ($total_users / $number); // get the total pages by dividing the total users to the maximum numbers of user to be displayed //Check if the total pages has a decimal we will add + 1 page
$total_pages = is_float($total_pages) ? intval($total_users / $number) + 1 : intval($total_users / $number);
if(is_array($query )){
foreach($query as $agent_data) {
echo '<tr>';
echo '<td>'.$agent_data->user_login.'</td>';
echo '<td>'.$agent_data->user_email.'</td>';
echo '<td><a href="https://www.gta-link.com/support/edit-agent/?uid='.$agent_data->ID.'">Edit</a></td>';
echo '</tr>';
}
}
?>
</table>
<?php
if ($total_users > $total_query) {
echo '<div id="support-pagination" class="clearfix">';
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => $current_page,
'total' => $total_pages,
'prev_next' => false,
'type' => 'list',
));
}
echo '</div>';
?>
</div>