编辑:这是我试图分页的当前代码。它创建一个自定义查询, 不包括最新帖子以及一个类别中的所有帖子。分页在大多数情况下都可以正常工作, 但是问题是分页列表中的最后一个链接将我带到了空白页。
<
section class="card-grid card-grid--push">
<
main class="container container--wide">
<
?php$current_page = get_query_var('paged');
$current_page = max( 1, $current_page );
$per_page = 3;
$offset = ( $current_page - 1 ) * $per_page + 1;
$post_list = new WP_Query(array(
'cat'=>
-15, 'posts_per_page' =>
$per_page, 'paged'=>
$current_page, 'offset'=>
$offset, 'orderby'=>
'date', 'order'=>
'DESC', ));
if ( $post_list->
have_posts() ):
while ( $post_list->
have_posts() ):
$post_list->
the_post();
?>
<
a href="http://www.srcmini.com/<
?php the_permalink();
?>" <
?php post_class('card');
?>
>
<
article class="card__content">
<
?php the_post_thumbnail('th-card');
?>
<
div class="card__head">
<
span class="category">
<
?php $category = get_the_category();
echo $category[0]->
cat_name;
?>
<
/span>
<
h2 class="card__title">
<
?php the_title();
?>
<
/h2>
<
/div>
<
/article>
<
/a>
<
?php endwhile;
?>
<
div class="pagination">
<
?php
echo paginate_links( array(
'total'=>
$post_list->
max_num_pages, 'current' =>
$current_page, 'type'=>
'list', 'prev_text' =>
'?', 'next_text' =>
'?'
) );
?>
<
/div>
<
?php
endif;
wp_reset_postdata();
?>
<
/main>
#1[编辑]这是经过全面测试并可以正常工作的地方:
$current_page = get_query_var('paged');
$current_page = max( 1, $current_page );
$per_page = 12;
$offset_start = 1;
$offset = ( $current_page - 1 ) * $per_page + $offset_start;
$post_list = new WP_Query(array(
'cat'=>
-15, 'posts_per_page' =>
$per_page, 'paged'=>
$current_page, 'offset'=>
$offset, // Starts with the second most recent post.
'orderby'=>
'date', // Makes sure the posts are sorted by date.
'order'=>
'DESC', // And that the most recent ones come first.
));
// Manually count the number of pages, because we used a custom OFFSET (i.e.
// other than 0), so we can't simply use $post_list->
max_num_pages or even
// $post_list->
found_posts without extra work/calculation.
$total_rows = max( 0, $post_list->
found_posts - $offset_start );
$total_pages = ceil( $total_rows / $per_page );
if ( $post_list->
have_posts() ):
while ( $post_list->
have_posts() ):
$post_list->
the_post();
// loop output here
endwhile;
echo paginate_links( array(
'total'=>
$total_pages, 'current' =>
$current_page, ) );
endif;
wp_reset_postdata();
【存在偏移量时,如何对WP_Query结果进行分页()】PS:使用制表符重新缩进了代码。
推荐阅读
- 如何防止弹出窗口在Mobile上放大()
- 我如何获得WordPress以了解我的自定义主题不是子主题()
- linux比较两个目录的差异
- #yyds干货盘点#Redis之Pub/Sub
- SpringCloud升级之路2020.0.x版-44.避免链路信息丢失做的设计
- ES6学习 第六章 数值的扩展
- k8s中imagePullSecrets
- 9000字加图文 | 带你了解IP地址划分子网掩码的作用实际中IP地址规划是怎么样的
- Linux基础之文件系统与日志分析