You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

自定义分类法归档页面不生效?排查与解决方法

问题排查与解决方案

核心问题分析

出现该问题的主要原因有两个:

  1. 自定义分类法注册时机早于自定义文章类型,导致分类法未正确关联到文章类型
  2. 自定义文章类型禁用了重写规则,干扰了分类法归档的URL解析

修正代码

1. 修正自定义文章类型注册代码

将rewrite参数从false改为带slug的数组,启用重写规则:

add_action( 'init', 'register_group_post_type' );
function register_group_post_type() {       
    register_post_type('book', array(
        'label'               => 'book',
        'labels'              => array(
            'name'          => 'Livre',
            'singular_name' => 'Livre',
            'menu_name'     => 'Livre',
            'all_items'     => 'All Livre',
            'add_new'       => 'Add Livre',
            'add_new_item'  => 'Add new Livre',
            'edit'          => 'Edit',
            'edit_item'     => 'Edit Livre',
            'new_item'      => 'New Livre',
        ),
        'description'         => 'Livre',
        'public'              => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_rest'        => true,
        'rest_base'           => '',
        'show_in_menu'        => true,
        'exclude_from_search' => false,
        'capability_type'     => 'post',
        'map_meta_cap'        => true,
        'menu_icon'           => 'dashicons-format-image',
        'hierarchical'        => false,
        'rewrite'             => array('slug' => 'book'), // 启用重写规则并设置slug
        'has_archive'         => true,
        'query_var'           => true,
        'supports'            => array( 'title', 'thumbnail','editor'),
    ) );  
}

2. 修正自定义分类法注册代码

调整注册优先级,确保在文章类型注册完成后再注册分类法:

// 将优先级从0改为11,晚于文章类型的默认优先级10
add_action( 'init', 'create_subjects_hierarchical_taxonomy', 11 );    
function create_subjects_hierarchical_taxonomy() {    
    $labels = array(
        'name' => _x( 'Book Categories ', 'taxonomy general name' ),
        'singular_name' => _x( 'Book Category', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Book Category' ),
        'all_items' => __( 'All Book Categories' ),
        'parent_item' => __( 'Parent Book Category' ),
        'parent_item_colon' => __( 'Parent Book Category:' ),
        'edit_item' => __( 'Edit Book Category' ), 
        'update_item' => __( 'Update Book Category' ),
        'add_new_item' => __( 'Add New Book Category' ),
        'new_item_name' => __( 'New Book Category Name' ),
        'menu_name' => __( 'Book Categories' ),  
    );    
  
    register_taxonomy('book-categories',array('book'), array(
        'hierarchical' => true,
        'labels' => $labels,
        'show_ui' => true,
        'show_in_rest' => true,
        'show_admin_column' => true,
        'query_var' => true,
        'rewrite' => array( 'slug' => 'book-categories' ),
    ));  
}

最终操作步骤

  1. 替换主题中现有的自定义文章类型和分类法注册代码为上述修正版本
  2. 进入WordPress后台 → 设置 → 固定链接,直接点击「保存更改」按钮(无需修改任何设置,目的是重新生成重写规则)
  3. 测试分类链接,此时应正确加载taxonomy-book-categories.php模板

内容的提问来源于stack exchange,提问作者libertarian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 13:02:18