我是php和word press的新手。我有一个问题,我正在使用sql query
在我的posts
从我的data base
. 现在的问题是,我的post
我不知道如何申请pagination
在使用SQL query
. 这是我在post
.
<?php
global $wpdb;
// QUERY HERE TO COUNT TOTAL RECORDS FOR PAGINATION
$total = $wpdb->get_var("SELECT COUNT(distinct hadith_no, content) FROM muslim");
$post_per_page = 10;
$page = isset( $_GET[\'cpage\'] ) ? abs( (int) $_GET[\'cpage\'] ) : 1;
$offset = ( $page * $post_per_page ) - $post_per_page;
// QUERY HERE TO GET OUR RESULTS
$results = $wpdb->get_results("SELECT hadith_no, content FROM muslim");
// PHP FOR EACH LOOP HERE TO DISPLAY OUR RESULTS
foreach($results as $row)
{
echo $row->hadith_no." ".$row->content."<br>";
}
// END OUR FOR EACH LOOP
?>
// PAGINATION HERE IN NICE BOOTSTRAP STYLES
<?php
echo \'<div class="pagination">\';
echo paginate_links( array(
\'base\' => add_query_arg( \'cpage\', \'%#%\' ),
\'format\' => \'\',
\'prev_text\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'total\' => ceil($total / $post_per_page),
\'current\' => $page,
\'type\' => \'list\'
));
echo \'</div>\';
?>
这给了我一个垂直分页的页面底部,仍然显示所有内容在一个单一的职位,而不是显示10个职位。任何能帮我使用
pagination
在我的
post
或者编辑此代码以便
pagination
.非常感谢。
SO网友:Salik Asad
<?php
global $wpdb;
// QUERY HERE TO COUNT TOTAL RECORDS FOR PAGINATION
$total = $wpdb->get_var("SELECT COUNT(*) FROM (SELECT * FROM muslim LIMIT 0,431) AS a");
$post_per_page = 10;
$page = isset( $_GET[\'cpage\'] ) ? abs( (int) $_GET[\'cpage\'] ) : 1;
$offset = ( $page * $post_per_page ) - $post_per_page;
// QUERY HERE TO GET OUR RESULTS
$results = $wpdb->get_results("SELECT hadith_no, content FROM muslim LIMIT $post_per_page OFFSET $offset");
// PHP FOR EACH LOOP HERE TO DISPLAY OUR RESULTS
foreach($results as $row)
{
echo $row->hadith_no." ".$row->content."<br>";
}
// END OUR FOR EACH LOOP
?>
<?php
echo \'<div class="pagination">\';
echo paginate_links( array(
\'base\' => add_query_arg( \'cpage\', \'%#%\' ),
\'format\' => \'\',
\'prev_text\' => __(\'«\'),
\'next_text\' => __(\'»\'),
\'total\' => ceil($total / $post_per_page),
\'current\' => $page,
\'type\' => \'list\'
));
echo \'</div>\';
?>
只需根据需要编辑sql查询,这将很好地工作。