WordPress使用$wpdb->get_结果自定义分页
我可以通过$wpdb->get_results从Wordpress数据库中获取自定义数据,比如so$results=$wpdb->get_results$query,OBJECT;但是,我想使用paginate_链接对数据进行分页 它目前没有显示带有分页链接的数据,我想我的错误可能在$results=$wpdb->get_results$query.“ORDER BY id DESC LIMIT”范围内$偏移量','$每个页面的项目,对象 我的代码:WordPress使用$wpdb->get_结果自定义分页,wordpress,Wordpress,我可以通过$wpdb->get_results从Wordpress数据库中获取自定义数据,比如so$results=$wpdb->get_results$query,OBJECT;但是,我想使用paginate_链接对数据进行分页 它目前没有显示带有分页链接的数据,我想我的错误可能在$results=$wpdb->get_results$query.“ORDER BY id DESC LIMIT”范围内$偏移量','$每个页面的项目,对象 我的代码: global $wpdb;
global $wpdb;
$table_name = $wpdb->prefix . 'templates';
$items_per_page = 3;
$offset = ( $page * $items_per_page ) - $items_per_page;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$query = 'SELECT * FROM '.$table_name;
$total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
$total = $wpdb->get_var( $total_query );
$results = $wpdb->get_results( $query.'ORDER BY id DESC LIMIT'. $offset.', '. $items_per_page, OBJECT );
$results = $wpdb->get_results( $query, OBJECT );
if(!empty($results)) {
echo"<table class=\"table table-hover\">";
echo"<thead>";
echo"<tr>";
echo"<th>Id</th>";
echo"<th>Date</th>";
echo"<th>Name</th>";
echo"<th>Image src</th>";
echo"<th>Category</th>";
echo"<th>Preview Link</th>";
echo"<th>BuiltWith</th>";
echo"<th>Price</th>";
echo"</tr>";
echo"</thead>";
echo"<tbody>";
foreach($results as $row){
echo"<tr>";
echo"<td>". $row->id . "</td>";
echo"<td>". $row->tdateTime ."</td>";
echo"<td>". $row->tName ."</td>";
echo"<td>". $row->tName ."</td>";
echo"<td>". $row->tCategory ."</td>";
echo"<td>". $row->tPreview ."</td>";
echo"<td>". $row->tBuiltWith . "</td>";
echo"<td>". $row->tPrice ."</td>";
echo"</tr>";
}
echo"</tbody>";
echo"</table>";
}
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $items_per_page),
'current' => $page
));
试试这个代码
<?php
ob_start();
$pagenum = isset( $_GET['pagenum'] ) ? absint( $_GET['pagenum'] ) : 1;
global $wpdb;
$table_name = $wpdb->prefix . "templates";
$limit = 10;
$offset = ( $pagenum - 1 ) * $limit;
$total = $wpdb->get_var("SELECT COUNT(id) FROM $table_name ");
$num_of_pages = ceil( $total / $limit );
?>
<?php
$questionnaire_data = $wpdb->get_results("SELECT * FROM $table_name desc LIMIT $offset, $limit", OBJECT );
$rowcount = $wpdb->num_rows;
?>
<h1>Result</h1>
<table class="wp-list-table widefat fixed striped" style="width: 99%;">
<tr>
<thead>
<th style="width: 5%;"><strong>S.No</strong></th>
</thead>
</tr>
<tbody>
<?php
if($rowcount ) {
$i=1;
foreach ($questionnaire_data as $key=>$singledata) {
echo "<tr class='no-items'>";
echo "<td>".$i."</td>";
echo "</tr>";
$i++;
}
} else {
echo "<td colspan='4' align='center'> No details(s) found </td>";
}
?>
</tbody>
<tfoot>
<tr>
<th style="width: 5%;"><strong>S.No</strong></th>
</tr>
</tfoot>
</table>
<?php
$page_links = paginate_links( array(
'base' => add_query_arg( 'pagenum', '%#%' ),
'format' => '',
'prev_text' => __( '«', 'text-domain' ),
'next_text' => __( '»', 'text-domain' ),
'total' => $num_of_pages,
'current' => $pagenum
) );
if ( $page_links ) {
echo '<div class="tablenav" style="width: 99%;"><div class="tablenav-pages" style="margin: 1em 0">' . $page_links . '</div></div>';
}
}
更新
我已经测试了这个,它在我的网站上运行。有几件事:
将我的$query替换为您的
全局$wpdb每个关于全局变量的注释,因为它超出了范围
get_results在未告知其他信息时返回一个对象。第二个参数是返回类型
代码如下:
如果最后一页的行数少于3行或第一页的行数少于3行,则最后一页将出现错误502。