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. The default in my settings is 20.

我已經讀過,如果“posts_per_page”設置為小於默認值,那么有一個錯誤會給你留下404錯誤,但這似乎不是問題。我的設置中的默認值為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!

3 个解决方案

#1


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 %% postname%在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. This fix saved me!

我花了很多時間閱讀有關此錯誤的不同原因和解決方案,在我的情況下,WP並沒有請求正確的自定義類別。這個修復救了我!

#2


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
                    ));

#3


I would try switching to WP_Query first, it's less buggy with Pagination.

我會首先嘗試切換到WP_Query,它與Pagination的錯誤不大。

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. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).

query_posts()是一種過於簡單化和有問題的方法來修改頁面的主要查詢,方法是用新的查詢實例替換它。它是低效的(重新運行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. 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.

如果這不起作用,請嘗試將永久鏈接結構更改為Post ID,並查看是否更改了它。如果這些都不起作用,請將$ cat設置為您知道的類別(並且有26個帖子),並確保不會導致問題。

Hope this helps.

希望這可以幫助。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/07/7da71822919d90ab2cb3a4d88fe5076e.html



 
  © 2014-2022 ITdaan.com