Wordpress $wpdb 自定义查询增加翻页

阅读 (793)
Wordpress 二次开发时用到一些自定义查询,查询结果集太长,需增加翻页功能

$wpdb->get_results() 查询结果增加翻页

global $wpdb;
$wpdb->query("SELECT * FROM _cwcustom_table");
$total = $wpdb->num_rows;
$per_page = 20;
$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 / $per_page),
    'current' => $page
));
$start_page = ($page-1) * $per_page;
 $result = $wpdb->get_results("SELECT * FROM _cwcustom_table LIMIT $start_page,$per_page");
 foreach( $result  as $table_field){
	echo '<tr>';
	echo '<td>'.$table_field->ID.'</td>';
	echo '<td>'.$table_field->CUSTOMER_FIRST_NAME.' ';
	echo $table_field->CUSTOMER_LAST_NAME.'</td>';
	echo '<td>'.$table_field->EMAIL.'</td>';
	echo '<td>'.$table_field->PHONE_NUMBER.'</td>';
	echo '<td>'.$table_field->DATE_CREATED.'</td>';
	echo '</tr>';
}

 

更新于:2015-09-01 01:52:43

留言

发表评论

标记为*的字段是必填项(邮箱地址不会被公开), 由于国内舆论控制, 评论需要管理员审核通过方可展示

返回顶部