我有一个文档用户的自定义表格,我想为它做分页,我尝试过以下代码,但我遇到了2个问题。
以下是分页代码:
<?php
global $wpdb;
$limit = 2;
if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page=1; };
$start_from = ($page-1) * $limit;
$selct_data = $wpdb->get_results("SELECT * FROM `wp_singining` LIMIT $start_from, $limit " , OBJECT);
?>
<table align="center" border="1" class="user-listing-table" id="main_listing">
<tr>
<th><input type="checkbox" name="check_all" class="check_all"></th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Status</th>
</tr>
<?php
foreach ($selct_data as $value) {
echo '<tr><td><input type="checkbox" name="check_element" class="check_element"></td><td>'.$value->f_name.'</td>';
echo '<td>'.$value->l_name.'</td>';
echo '<td>'.$value->email.'</td>';
echo '<td>'.$value->phone.'</td>';
echo '<td>'.$value->status.'</td></tr>';
}
?>
</table>
<?php
$selct_data_ra = $wpdb->get_results("SELECT COUNT(id) as cnt FROM `wp_singining`
", OBJECT);
$total_records = $selct_data_ra[0]->cnt;
$total_pages = ceil($total_records / $limit);
$pagLink = "<nav><ul class='pagination'>";
for ($i=1; $i<=$total_pages; $i++) {
$pagLink .= "<li><a href='?page=".$i."'>".$i."</a></li>";
};
echo $pagLink . "</ul></nav>";
?>
以下代码用于重定向到下一页,我已在functions.php
文件中写道:
function custom_rewrite_rule(){
global $wp;
$wp->add_query_var( 'page' );
add_rewrite_rule('user-listing/(/([^/]+))?(/([^/]+))?/?','index.php?pagename=user-listing&page=$1','top' );
}
add_action('init', 'custom_rewrite_rule', 10, 0);
我很困惑,我做错了分页和重定向无法正常工作请帮助我。