我可以通过$ wpdb-> get_results这样从Wordpress数据库中获取自定义数据,例如$results = $wpdb->get_results( $query, OBJECT );
,但是我想使用paginate_links();
来对数据进行分页
目前没有显示带有分页链接的数据,我认为我的错误可能在$results = $wpdb->get_results( $query.'ORDER BY id DESC LIMIT'. $offset.', '. $items_per_page, OBJECT );
内
我的代码:
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
));
答案 0 :(得分:0)
尝试此代码
<?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>';
}
}
答案 1 :(得分:0)
更新
我已经对此进行了测试,并且可以在我的网站上使用。一些事情:
用您的$ query替换我的$ query
全局$ wpdb(根据您对全局变量的评论),因为它不在范围内!
get_results()在没有其他说明时返回对象(第二个参数是返回类型)
代码如下:
<?php
global $wpdb;
$table_name = $wpdb->prefix . 'templates';
$query = "(SELECT * FROM '.$table_name)";
$total_query = "SELECT COUNT(1) FROM (${query}) AS combined_table";
$total = $wpdb->get_var( $total_query );
$items_per_page = 3;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$offset = ( $page * $items_per_page ) - $items_per_page;
$results = $wpdb->get_results( $query . " ORDER BY post_date LIMIT ${offset}, ${items_per_page}" );
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
));
?>