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

如何让Wordpress自定义文章类型编辑页显示taxonomy选择框

问题:自定义文章类型关联分类法后编辑页不显示分类选择框

我在WordPress中创建了名为video的自定义文章类型(custom post type),并为其关联了新的video_category分类法(taxonomy)。能在后台导航菜单中找到video_category并创建新分类,但在添加或编辑video文章时,编辑页面并未显示该分类的选择列表。请问需要如何调整配置,才能让用户在新建或编辑video页面中为文章关联一个或多个video_category?

当前使用的代码

function video_register() {   
    $labels = array( 
        'name'                  => _x('Video', 'post type general name'), 
        'singular_name'         => _x('Video Item', 'post type singular name'), 
        'add_new'               => _x('Add New', 'work item'), 
        'add_new_item'          => __('Add New video Item'), 
        'edit_item'             => __('Edit video Item'), 
        'new_item'              => __('New Video Item'), 
        'view_item'             => __('View Video Item'), 
        'search_items'          => __('Search Video'), 
        'not_found'             => __('Nothing found'), 
        'not_found_in_trash'    => __('Nothing found in Trash'), 
        'parent_item_colon'     => '' 
    );   

    $args = array( 
        'labels'                => $labels, 
        'public'                => true, 
        'publicly_queryable'    => true, 
        'show_ui'               => true,
        'show_in_rest'          => true,
        'query_var'             => true, 
        'rewrite'               => array('slug' => 'videos-list', 'with_front'=> false), 
        'capability_type'       => 'post', 
        'hierarchical'          => false, 
        'menu_position'         => null, 
        'supports'              => array('title','editor','thumbnail','excerpt'),
        'taxonomies'            => array('video_category')
    );   

    register_post_type('video', $args); 
}
add_action('init', 'video_register');

function create_video_category_taxonomy() {
    $labels = array(
        'name'              => _x('Video Categories', 'taxonomy general name'),
        'singular_name'     => _x('Video Category', 'taxonomy singular name'),
        'search_items'      => __('Search Video Categories'),
        'all_items'         => __('All Video Categories'),
        'parent_item'       => __('Parent Video Category'),
        'parent_item_colon' => __('Parent Video Category:'),
        'edit_item'         => __('Edit Video Category'),
        'update_item'       => __('Update Video Category'),
        'add_new_item'      => __('Add New Video Category'),
        'new_item_name'     => __('New Video Category Name'),
        'menu_name'         => __('Video Categories'),
        'show_in_rest'      => true,
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'show_in_menu'      => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'video-category'),
    );

    register_taxonomy('video_category', 'video', $args);
}
add_action('init', 'create_video_category_taxonomy');

解决方案及修正代码

核心问题是自定义文章类型和分类法的注册顺序:必须先注册分类法,再注册关联它的自定义文章类型,否则WordPress无法正确识别关联关系。

调整两个函数的init钩子优先级,让分类法先执行(优先级数值越小,执行越早):

// 先注册分类法,设置优先级为9确保先执行
function create_video_category_taxonomy() {
    $labels = array(
        'name'              => _x('Video Categories', 'taxonomy general name'),
        'singular_name'     => _x('Video Category', 'taxonomy singular name'),
        'search_items'      => __('Search Video Categories'),
        'all_items'         => __('All Video Categories'),
        'parent_item'       => __('Parent Video Category'),
        'parent_item_colon' => __('Parent Video Category:'),
        'edit_item'         => __('Edit Video Category'),
        'update_item'       => __('Update Video Category'),
        'add_new_item'      => __('Add New Video Category'),
        'new_item_name'     => __('New Video Category Name'),
        'menu_name'         => __('Video Categories'),
        'show_in_rest'      => true,
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'show_in_menu'      => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'video-category'),
    );

    register_taxonomy('video_category', 'video', $args);
}
add_action('init', 'create_video_category_taxonomy', 9);

// 后注册自定义文章类型,优先级默认10
function video_register() {   
    $labels = array( 
        'name'                  => _x('Video', 'post type general name'), 
        'singular_name'         => _x('Video Item', 'post type singular name'), 
        'add_new'               => _x('Add New', 'work item'), 
        'add_new_item'          => __('Add New video Item'), 
        'edit_item'             => __('Edit video Item'), 
        'new_item'              => __('New Video Item'), 
        'view_item'             => __('View Video Item'), 
        'search_items'          => __('Search Video'), 
        'not_found'             => __('Nothing found'), 
        'not_found_in_trash'    => __('Nothing found in Trash'), 
        'parent_item_colon'     => '' 
    );   

    $args = array( 
        'labels'                => $labels, 
        'public'                => true, 
        'publicly_queryable'    => true, 
        'show_ui'               => true,
        'show_in_rest'          => true,
        'query_var'             => true, 
        'rewrite'               => array('slug' => 'videos-list', 'with_front'=> false), 
        'capability_type'       => 'post', 
        'hierarchical'          => false, 
        'menu_position'         => null, 
        'supports'              => array('title','editor','thumbnail','excerpt'),
        'taxonomies'            => array('video_category')
    );   

    register_post_type('video', $args); 
}
add_action('init', 'video_register', 10);

额外说明

  • 你已经在分类法参数中设置了show_in_rest => true,这确保了Gutenberg编辑器能正常显示分类选择框;如果使用经典编辑器,这个参数不是必须的,但保留也没问题。
  • 刷新WordPress后台的固定链接设置(设置→固定链接,直接点击保存即可),确保重写规则生效,避免前端访问分类页面出现404错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 11:22:08