简体   繁体   English

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

[英]Change number of posts on consecutive category pages (Wordpress)

I am trying change the number of posts that are displayed on a category pages to change on consecutive pages (page 2, 3, etc). 我正在尝试更改在连续页面上更改类别页面上显示的帖子数量(第2,3页等)。 So page one displays 7 posts, but pages 2, 3 and 4, etc of that category display only 6 posts per page (ie when you click 'next page' to list the older posts). 因此,第一页显示7个帖子,但该类别的第2,3和4页等每页仅显示6个帖子(即,当您单击“下一页”列出较旧的帖子时)。

I am aware that it is relatively straightforward to change the number of posts for different categories / archive pages - but this is different, as I would like the paginated pages to have different numbers of posts. 我知道更改不同类别/存档页面的帖子数量相对简单 - 但这是不同的,因为我希望分页页面具有不同数量的帖子。

Any ideas? 有任何想法吗?

This is from an answer I recently done on WPSE. 这是我最近在WPSE上做的回答。 I have made some changes to suite your needs. 我已经做了一些改变以满足您的需求。 You can go and check out that post here 您可以在这里查看该帖子

STEP 1 第1步

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; 

    ?>

STEP 2 第2步

Use pre_get_posts to alter the main query to change the posts_per_page parameter on the category pages 使用pre_get_posts更改主查询以更改类别页面上的posts_per_page参数

STEP 3 第3步

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. 现在,从后端(我假设为6)设置posts_per_page选项,并设置我们将要使用的offset That will be 1 as you will need 7 posts on page one and 6 on the rest 那将是1因为你需要在第一页上有7个帖子,在其余时间需要6个帖子

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

STEP 4 第四步

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. 在第一页上,您需要向posts_per_page添加offsetposts_per_page可添加7个,以便在第一页上获得您的七个帖子。

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

STEP 5 第5步

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 = $offset + ( ($query->query_vars['paged']-1) * $ppp );
$query->set('posts_per_page',$ppp);
$query->set('offset',$offset); 

STEP 6 第6步

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 最后,您需要从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 );

ALL TOGETHER 全部一起

This is how your complete query will look like that should go into functions.php 这就是你的完整查询应该如何进入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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM