更改连续类别页面上的帖子数量(Wordpress) [英] Change number of posts on consecutive category pages (Wordpress)

查看:26
本文介绍了更改连续类别页面上的帖子数量(Wordpress)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将类别页面上显示的帖子数量更改为在连续页面(第 2、3 页等)上更改.因此,第一页显示 7 个帖子,但该类别的第 2、3 和 4 页等每页仅显示 6 个帖子(即,当您点击下一页"以列出较旧的帖子时).

I am trying change the number of posts that are displayed on a category pages to change on consecutive pages (page 2, 3, etc). So page one displays 7 posts, but pages 2, 3 and 4, etc of that category display only 6 posts per page (i.e. when you click 'next page' to list the older posts).

我知道更改不同类别/存档页面的帖子数量相对简单 - 但这是不同的,因为我希望分页页面具有不同数量的帖子.

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.

有什么想法吗?

推荐答案

这是我最近在 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 );

这篇关于更改连续类别页面上的帖子数量(Wordpress)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆