很早之前发过一个教程,意思就是置顶的文章算在每页的文章数量内
修改前

修改后

更新记录
这次主要修复了的问题是:比如我现在有23篇文章,置顶了3篇文章,美页显示10篇,如果不启用这个代码,分页是两页,启用后,则应该是分3页,但是当我访问2和3页的时候,不显示3的页码按钮
完善后的代码
function modify_pre_get_posts($query) {
if ($query->is_home() && $query->is_main_query()) {
$sticky_posts = get_option('sticky_posts');
$sticky_count = count($sticky_posts);
$posts_per_page = get_option('posts_per_page');
if (!$query->is_paged()) {
if ($sticky_count > 0) {
$query->set('posts_per_page', $posts_per_page - $sticky_count);
}
} else {
if (!empty($sticky_posts)) {
$query->set('post__not_in', $sticky_posts);
$offset = ( $query->query_vars['paged'] - 1 ) * $posts_per_page - count($sticky_posts);
$query->set('offset', $offset);
}
}
}
}
add_action('pre_get_posts', 'modify_pre_get_posts');
function adjust_pagination() {
if (is_home()) {
global $wp_query;
$sticky_posts = get_option('sticky_posts');
$sticky_count = count($sticky_posts);
$posts_per_page = get_option('posts_per_page');
// 获取非置顶文章总数
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => $sticky_posts,
'fields' => 'ids',
'posts_per_page' => -1,
'no_found_rows' => true,
);
$non_sticky_query = new WP_Query($args);
$non_sticky_count = $non_sticky_query->post_count;
// 第1页显示的普通文章数
$first_page_posts = $posts_per_page - $sticky_count;
// 剩余普通文章数
$remaining_posts = $non_sticky_count - $first_page_posts;
// 计算总页数:第1页 + 剩余页数
if ($remaining_posts > 0) {
$total_pages = 1 + ceil($remaining_posts / $posts_per_page);
} else {
$total_pages = 1;
}
$wp_query->max_num_pages = $total_pages;
}
}
add_action('wp', 'adjust_pagination');



![表情[yun]-WordPress主题模板-zibll子比主题](https://www.zibll.com/wp-content/themes/zibll/img/smilies/yun.gif)
![表情[aoman]-WordPress主题模板-zibll子比主题](https://www.zibll.com/wp-content/themes/zibll/img/smilies/aoman.gif)
![表情[xiaoku]-WordPress主题模板-zibll子比主题](https://www.zibll.com/wp-content/themes/zibll/img/smilies/xiaoku.gif)

![表情[xieyanxiao]-WordPress主题模板-zibll子比主题](https://www.zibll.com/wp-content/themes/zibll/img/smilies/xieyanxiao.gif)



