使用此解决方案:Paginate result set from $wpdb->get_results()
我确实为作者列表页面模板编写了代码,如下所示:
<div>
<table id="mytable"><tbody>
<?php
// Get the authors from the database ordered by user\'s display name
global $wpdb;
$query = "SELECT ID, display_name from $wpdb->users ORDER BY display_name LIMIT 10";
$author_ids = $wpdb->get_results($query);
$authcount = -1;
$total = $wpdb->get_var("
SELECT COUNT(ID)
FROM $wpdb->users
");
$authors_per_page = 10;
$page = isset( $_GET[\'cpage\'] ) ? abs( (int) $_GET[\'cpage\'] ) : 1;
echo paginate_links( array(
\'base\' => add_query_arg( \'cpage\', \'%#%\' ),
\'format\' => \'\',
\'prev_text\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'total\' => ceil($total / $authors_per_page),
\'current\' => $page
));
// Loop through each author
foreach($author_ids as $author) :
// Get user data
$curauth = get_userdata($author->ID);
// If user_ID is 1 or 24, remove them from list
if( ! in_array($curauth->ID, array(1, 24 )) ) :
// Get link to author page
$user_link = get_author_posts_url($curauth->ID);
$post_count = get_usernumposts($curauth->ID);
?>
<tr<?php echo $authcount % 2 ? \' class="odd"\' : \'\'; ?>>
<td>
<?php echo $authcount; ?>
</td><th scope="row">
<a href="<?php echo $user_link; ?>" title="<?php echo $curauth->display_name; ?>"><?php echo $curauth->display_name; ?></a>
</th><td>
( <?php echo $post_count; ?> )
</td></tr>
<?php endif; ?>
<?php $authcount = $authcount + 1; ?>
<?php endforeach; ?>
</tbody></table><br /></div>
但每个页码链接只显示从1到10的作者列表,而数据库中至少有50位作者。请帮我找出错误并改正。非常感谢。