简体   繁体   English

Wordpress category.php 分页 404 错误

[英]Wordpress category.php Pagination 404 Errors

I'm trying to display posts for categories in my category.php file with pagination, but when I click the "older posts" button, I'm getting a 404. Here's the code I'm currently using for the query:我正在尝试使用分页显示 category.php 文件中类别的帖子,但是当我单击“旧帖子”按钮时,我收到 404。这是我目前用于查询的代码:

<?php

    // Get ID of category we're currently looking at
    $cat = get_cat_id( single_cat_title("",false) );

    query_posts(array(
        'posts_per_page'=>25,
        'cat' => $cat,
        'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 )
    ));
    if(have_posts()):
?>

The permalink structure I'm using is /%category%/%postname%/我使用的固定链接结构是 /%category%/%postname%/

I've read that there is a bug that will leave you with a 404 error if the "posts_per_page" is set to less than the default, but that doesn't seem to be the issue.我读过有一个错误,如果“posts_per_page”设置为小于默认值,则会出现 404 错误,但这似乎不是问题。 The default in my settings is 20.我的设置中的默认值为 20。

Any ideas?有任何想法吗? Is this an issue with the permalink settings?这是永久链接设置的问题吗? Shouldn't having /category-name/page/2 work the same way as /blog-page/page/2 works?不应该让 /category-name/page/2 的工作方式与 /blog-page/page/2 的工作方式相同吗?

I also get a 404 if I try to access categories like this: /category/cat-name, or /blog-page/category/cat-name如果我尝试访问这样的类别,我也会得到 404:/category/cat-name 或 /blog-page/category/cat-name

Thanks!谢谢!

This sounds very similar to what I experienced (In my child theme, custom permalink %category%%postname% resulted in a 404 when using pagination in category.php, only on page 2 though).这听起来与我所经历的非常相似(在我的子主题中,自定义永久链接 %category%%pos​​tname% 在 category.php 中使用分页时导致 404,但仅在第 2 页)。

I found this solution worked really well: http://www.bamboosolutions.co.uk/fix-404-errors-wordpress-pagination/我发现这个解决方案非常有效: http : //www.bamboosolutions.co.uk/fix-404-errors-wordpress-pagination/

To summarize the solution, in my child functions.php file I added this bit of code:总结一下解决方案,在我的子functions.php文件中,我添加了以下代码:

function custom_pre_get_posts( $query ) {  
if( $query->is_main_query() && !$query->is_feed() && !is_admin() && is_category()) {  
    $query->set( 'paged', str_replace( '/', '', get_query_var( 'page' ) ) );  }  } 

add_action('pre_get_posts','custom_pre_get_posts'); 

function custom_request($query_string ) { 
     if( isset( $query_string['page'] ) ) { 
         if( ''!=$query_string['page'] ) { 
             if( isset( $query_string['name'] ) ) { unset( $query_string['name'] ); } } } return $query_string; } 

add_filter('request', 'custom_request');

I spent so much time reading about different causes and solutions for this error, in my case it was that WP was not requesting the correct custom category.我花了很多时间阅读有关此错误的不同原因和解决方案,就我而言,WP 没有请求正确的自定义类别。 This fix saved a lot of my time!这个修复节省了我很多时间!

I had the same issue, and Lauren's fix helped me.我遇到了同样的问题,劳伦的修复帮助了我。 My problem was that with this code the current page didn't change, it got stuck on page 1.我的问题是,使用此代码,当前页面没有改变,它卡在了第 1 页。

In functions.php i added the following code:在functions.php中,我添加了以下代码:

function custom_pre_get_posts($query)
{
    if ($query->is_main_query() && !$query->is_feed() && !is_admin() && is_category()) {
        $query->set('page_val', get_query_var('paged'));
        $query->set('paged', 0);
    }
}

add_action('pre_get_posts', 'custom_pre_get_posts');

and in category template (category.php) i used this code:在类别模板(category.php)中,我使用了以下代码:

  $paged = (get_query_var('page_val') ? get_query_var('page_val') : 1);
  $query = new WP_Query(array(
    'posts_per_page' => 3,
    'cat' => $cat,
    'orderby' => 'date',
    'paged' => $paged,
    'order' => 'DESC'));

for pagination i changed this code like this.对于分页,我像这样更改了此代码。 Hope my solution helps:希望我的解决方案有帮助:

$big = 999999999;  
echo paginate_links(array(
                        'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
                        'format' => '/page/%#%',
                        'current' => max(1, $paged),
                        'prev_text' => __('Previous Page'),
                        'next_text' => __('Next Page'),
                        'show_all' => true,
                        'total' => $query->max_num_pages
                    ));

I would try switching to WP_Query first, it's less buggy with Pagination.我会先尝试切换到 WP_Query,它的分页问题较少。

https://codex.wordpress.org/Function_Reference/query_posts https://codex.wordpress.org/Function_Reference/query_posts

query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. query_posts() 是通过用新的查询实例替换页面的主查询来修改页面的主查询的过于简单和有问题的方法。 It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).它效率低下(重新运行 SQL 查询)并且在某些情况下会彻底失败(尤其是在处理帖子分页时)。

$cat = get_cat_id( single_cat_title("",false) );
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$the_query = new  WP_Query
 (
          array
          (
              'posts_per_page'=>25,
              'cat' => $cat,
              'paged' => $paged
          ),
);
if ($the_query->have_posts()) : ?>

If that doesn't work, try changing your permalink structure to Post ID and see if that changes it.如果这不起作用,请尝试将您的永久链接结构更改为 Post ID,看看是否会改变它。 If neither of those work, set $cat to a category you know exists (and has 26 posts) and make sure that's not causing the problem.如果这些都不起作用,请将 $cat 设置为您知道存在的类别(并且有 26 个帖子),并确保这不会导致问题。

Hope this helps.希望这可以帮助。

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

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