【问题标题】:Wordpress category.php page pagination showing 404 pageWordpress category.php 页面分页显示 404 页面
【发布时间】:2015-06-10 16:06:26
【问题描述】:

在 category.php 页面中,默认帖子显示在带有分页的特定类别中,并且从永久链接设置中删除了 url 中的“类别”。

不幸的是,它在分页到第二页时重定向到 404 页面。

我什至尝试检查 wordpress 默认 2015 主题中的类别分页,它在那里也不起作用。

这是我的 category.php,只是用 wordpress 循环进行测试。

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

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

注意:如果没有从 url 中删除类别,则一切正常。

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

我是否需要添加/修改任何内容才能使其正常工作?

【问题讨论】:

标签: php wordpress pagination


【解决方案1】:

这是正常的,请尝试使用下面的代码:

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

重要编辑:分页存在错误,第 10 页以上无法正常工作。
现已修复:(\d)+ 更改为 (\d+)

希望这会有所帮助:)

【讨论】:

    猜你喜欢
    • 2015-07-18
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    相关资源
    最近更新 更多