简体   繁体   English

设置posts_per_page时分页/ page / 2 /的WordPress 404错误

[英]WordPress 404 error with pagination /page/2/ when setting posts_per_page

I am fairly new to coding with WordPress. 我对使用WordPress编码非常陌生。 Essentially what my intention was is to set different post per page limits for separate categories. 本质上,我的目的是为单独的类别设置不同的每页帖子数限制。

so I edited the functions.php file on my theme with the following code. 所以我用以下代码编辑了主题上的functions.php文件。

// Set the posts per category.
add_filter('pre_get_posts', 'per_category_basis');
function per_category_basis($query){

    if ($query->is_category) {
        // category named 'books' show 12 posts
        if (is_category('team')){
            $query->set('posts_per_page', 100);
        }

        if (is_category('our-work') || is_category('news')){
            $query->set('posts_per_page', 2);
        }
    }
    return $query;
}

this does work perfectly for displaying the number of posts. 这确实适合显示帖子数。 The problem is when navigating to page 2 website.com/news/page/2/ I will get a 404 error. 问题是导航到第2页website.com/news/page/2/时,我会收到404错误。

I have already attempted changes to the function.php and searched google for other options. 我已经尝试过更改function.php,并在google中搜索其他选项。 I've changed the settings under reading and still no luck. 我已经更改了阅读下的设置,但仍然没有运气。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I am aware there is a 7 year old plugin called category pagination fix. 我知道有一个7年旧的插件,称为类别分页修复。 but it is out dated and doesn't work with PHP 7.2 and latest version of WordPress. 但是它已经过时,不能在PHP 7.2和最新版本的WordPress上使用。

Ok, so I found out the issue. 好的,所以我找到了问题所在。

The reason why this page was displaying a 404 is because under settings > permalinks i was using the custom structure 该页面显示404的原因是因为我在设置>永久链接下使用了自定义结构

/%category%/%postname%/ /%类别%/%postname%/

and had the "category base" set to "." 并将“类别库”设置为“”。 (a fullstop) in term this removed the pages from being /category/news/page/2 to /news/page/2 but also broke the pagination.. (句号)将页面从/ category / news / page / 2移至/ news / page / 2,但也破坏了分页。

The easiest fix i found was simply installing the below plugin. 我找到的最简单的修复方法就是安装以下插件。 Fixed my issue and made pagination work 100% correctly. 解决了我的问题,并使分页正确地工作100%。

There is also a similar function in Yoast SEO pro which may do similar. Yoast SEO pro中也有类似的功能,可能会做类似的事情。

No Category Base (WPML): https://wordpress.org/plugins/no-category-base-wpml/ 没有类别库(WPML): https : //wordpress.org/plugins/no-category-base-wpml/

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

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