有偏移量时如何对 WP_Query 结果进行分页?
How can I paginate WP_Query results when there's an offset?
编辑:这是我正在尝试分页的当前代码。它创建一个自定义查询,排除最新的 post,以及一个类别中的所有 post。分页在大多数情况下工作正常,但问题是分页列表中的最后一个 link 将我带到空白页。
<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="<?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>
[编辑] 在这里,经过全面测试和工作:
$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();
PS:代码是 re-indented 使用制表符。
这是我找到的 lil 修复程序;将负 1 添加到总页数。
我是说改变
'total' => $total_pages,
至
'total' => ($total_pages - 1),
我在发布前进行了测试。谢谢大家之前的回答;它使我免于头痛!
编辑:这是我正在尝试分页的当前代码。它创建一个自定义查询,排除最新的 post,以及一个类别中的所有 post。分页在大多数情况下工作正常,但问题是分页列表中的最后一个 link 将我带到空白页。
<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="<?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>
[编辑] 在这里,经过全面测试和工作:
$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();
PS:代码是 re-indented 使用制表符。
这是我找到的 lil 修复程序;将负 1 添加到总页数。 我是说改变
'total' => $total_pages,
至
'total' => ($total_pages - 1),
我在发布前进行了测试。谢谢大家之前的回答;它使我免于头痛!