dtyqflrr775518 2015-04-06 09:02 采纳率: 100%
浏览 201
已采纳

Wordpress category.php页面分页显示404页面

In category.php page default posts are shown from certain category with pagination, and 'category' is removed in url from permalinks settings.

Unfortunately it redirects to 404 page while paginating to second page.

I even tried to check category pagination in wordpress default 2015 theme it wont work in there too.

Here is my category.php just with wordpress loop for testing.

if ( have_posts() ) :
    while ( have_posts() ) : the_post();
        the_title(); 
    endwhile;
endif;

next_posts_link( 'Older posts' );
previous_posts_link( 'Newer posts' );

Note: everything works well if category is not removed from url.

http://www.domain.com/category/my_category/page/2  (works)
http://www.domain.com/my_category/page/2  (doesnt work)

Do i need to add/modify anything to make it work ?

  • 写回答

1条回答 默认 最新

  • douxian7534 2015-07-14 02:11
    关注

    This is normal, please try using code below:

    add_filter( 'category_rewrite_rules', 'vipx_filter_category_rewrite_rules' );
    function vipx_filter_category_rewrite_rules( $rules ) {
        $categories = get_categories( array( 'hide_empty' => false ) );
    
        if ( is_array( $categories ) && ! empty( $categories ) ) {
            $slugs = array();
            foreach ( $categories as $category ) {
                if ( is_object( $category ) && ! is_wp_error( $category ) ) {
                    if ( 0 == $category->category_parent ) {
                        $slugs[] = $category->slug;
                    } else {
                        $slugs[] = trim( get_category_parents( $category->term_id, false, '/', true ), '/' );
                    }
                }
            }
    
            if ( ! empty( $slugs ) ) {
                $rules = array();
    
                foreach ( $slugs as $slug ) {
                    $rules[ '(' . $slug . ')/feed/(feed|rdf|rss|rss2|atom)?/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
                    $rules[ '(' . $slug . ')/(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
                    $rules[ '(' . $slug . ')(/page/(\d+)/?)?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[3]';
                }
            }
        }
        return $rules;
    }
    

    IMPORTANT EDIT: There was a bug in pagination, pages 10+ didn't work properly.
    Now fixed: (\d)+ changed to (\d+) .

    Hope this will be helpful :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分