这是我最近在 WPSE 上做的一个回答。我做了一些更改以满足您的需求。你可以去看看那个帖子here
第 1 步
如果您已将主查询更改为自定义查询,请将其改回默认循环
<?php
if ( have_posts() ) :
// Start the Loop.
while ( have_posts() ) : the_post();
///<---YOUR LOOP--->
endwhile;
//<---YOUR PAGINATION--->
else :
//NO POSTS FOUND OR SOMETHING
endif;
?>
第 2 步
使用pre_get_posts 更改主查询以更改类别页面上的posts_per_page 参数
第 3 步
现在,从后端获取posts_per_page 选项集(我假设为6),并设置我们将要使用的offset。那将是1,因为您需要在第一页发布 7 个帖子,在其余部分发布 6 个帖子
$ppg = get_option('posts_per_page');
$offset = 1;
第 4 步
在第一页,您需要将 offset 添加到 posts_per_page 将加起来为 7,才能在第一页获得您的 7 个帖子。
$query->set('posts_per_page', $offset + $ppp);
第 5 步
您必须将您的offset 应用到所有后续页面,否则您将在下一页上重复该页面的最后一个帖子
$offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('posts_per_page',$ppp);
$query->set('offset',$offset);
第 6 步
最后,你需要从found_posts 中减去你的偏移量,否则你在最后一页的分页会出错,并给你一个404 错误,因为最后一个帖子会因为帖子数不正确而丢失
function category_offset_pagination( $found_posts, $query ) {
$offset = 1;
if( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$found_posts = $found_posts - $offset;
}
return $found_posts;
}
add_filter( 'found_posts', 'category_offset_pagination', 10, 2 );
齐心协力
这就是你的完整查询应该进入functions.php的样子
function ppp_and_offset_category_page( $query ) {
if ($query->is_category() && $query->is_main_query() && !is_admin()) {
$ppp = get_option('posts_per_page');
$offset = 1;
if (!$query->is_paged()) {
$query->set('posts_per_page',$offset + $ppp);
} else {
$offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('posts_per_page',$ppp);
$query->set('offset',$offset);
}
}
}
add_action('pre_get_posts','ppp_and_offset_category_page');
function category_offset_pagination( $found_posts, $query ) {
$offset = 1;
if( !is_admin() && $query->is_category() && $query->is_main_query() ) {
$found_posts = $found_posts - $offset;
}
return $found_posts;
}
add_filter( 'found_posts', 'category_offset_pagination', 10, 2 );