在WordPress中开发一个插件并获得好处,但坚持插件页面的分页。这是我从互联网上下载的代码(从这里得到参考)
$items = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->review_media GROUP BY post_id")); // number of total rows in the database
//测试并获得评论结果 的print_r($项目); //说这是输出值2 echo $ rm_options ['list_per_page']; //这是我的选项设置为值1
if($items > 0) {
$p = new pagination;
$p->items($items);
$p->limit(empty($rm_options['list_per_page']) ? 20 : $rm_options['list_per_page']); // Limit entries per page
$p->target("admin.php?page=moderate.php");
$p->currentPage($_GET[$p->paging]); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(1); //No. of page away from the current page
if(!isset($_GET['paging'])) {
$p->page = 1;
} else {
$p->page = $_GET['paging'];
}
//Query for limit paging
$limit = "LIMIT " . ($p->page - 1) * $p->limit . ", " . $p->limit;
} else {
echo "No Record Found";
}
当我不通过post_id对我的查询进行分组时,它工作正常,但只要我将其行为分组就很奇怪了。它正在创建分页链接并获取空白页面。我认为原因是对行进行分组。但不知道如何解决这个问题。
这是我的表截图
非常感谢你的帮助......
答案 0 :(得分:0)
如果您使用WordPress WP_List_Table
类,它将处理分页并为用户提供默认的表体验。
此tutorial涵盖了所有内容,分页部分为:
function prepare_items() {
[...]
$per_page = 5;
$current_page = $this->get_pagenum();
$total_items = count($this->example_data);
// only necessary because we have sample data
$this->found_data = array_slice($this->example_data,(($current_page-1)*$per_page),$per_page);
$this->set_pagination_args( array(
'total_items' => $total_items, //WE have to calculate the total number of items
'per_page' => $per_page //WE have to determine how many items to show on a page
) );
$this->items = $this->found_data;
}
教程中显示here's the full plugin。在此other plugin中,您可以使用数据库数据查看WP_List_Table
。