且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改连续类别页面上的帖子数量(Wordpress)

更新时间:2023-12-01 23:20:28

这是我最近在 WPSE 上做的一个回答.我进行了一些更改以满足您的需求.您可以在此处

This is from an answer I recently done on WPSE. I have made some changes to suite your needs. You can go and check out that post here

第一步

如果您更改了自定义查询的主查询,请将其改回默认循环

If you have changed the main query for a custom query, change it back to the default loop

<?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 参数

Use pre_get_posts to alter the main query to change the posts_per_page parameter on the category pages

第 3 步

现在,从后端设置 posts_per_page 选项(我假设是 6),并设置我们将要使用的 offset.那将是 1,因为您需要在第一页上发表 7 篇文章,其余部分需要 6 篇

Now, get the posts_per_page option set from the back end (which I assume is 6) and also set your offset which we are going to use. That will be 1 as you will need 7 posts on page one and 6 on the rest

$ppg = get_option('posts_per_page');
$offset = 1;

第 4 步

在第一页上,您需要将 offset 添加到 posts_per_page 将加起来为 7,以使您的七个帖子出现在第一页上.

On page one, you'll need to add the offset to posts_per_page will add up to 7 to get your seven posts on page one.

$query->set('posts_per_page', $offset + $ppp);

步骤 5

您必须将 offset 应用到所有后续页面,否则您将在下一页重复该页面的最后一篇文章

You must apply your offset to all subsequent pages, otherwise you will get a repetition of the last post of the page on the next page

$offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('posts_per_page',$ppp);
$query->set('offset',$offset); 

步骤 6

最后,您需要从 found_posts 中减去您的偏移量,否则您在最后一页的分页将是错误的,并且由于最后一个帖子将丢失而给您一个 404 错误由于帖子数量不正确

Lastly, you need to subtract your offset from found_posts otherwise your pagination on the last page will be wrong and give you a 404 error as the last post will be missing due to the incorrect post count

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的样子

This is how your complete query will look like that should go into 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 );