WordPress自定义文章类型编辑页分类法显示错误问题求助
问题描述
自定义文章类型「blog」的编辑页面错误显示默认文章(Post)的分类法,而非关联的自定义分类法「categories」;但快速编辑区块可正常显示对应分类法:
- 快速编辑正常显示自定义分类法:

- 编辑页面错误显示默认分类:

当前使用的注册代码如下:
add_action( 'init', function() { register_taxonomy( 'categories', array( 'blog' ), [ 'labels' => [ 'name' => 'Blog Categories', 'singular_name' => 'Blog Category', ], 'public' => true, 'hierarchical' => true, 'show_in_rest' => true, 'show_admin_column' => true, ] ); register_post_type( 'blog', array( 'labels' => array( 'name' => 'Blog posts', 'singular_name' => 'Blog post' ), 'public' => true, 'has_archive' => true, 'show_in_rest' => true, 'supports' => array('title','editor', 'thumbnail', 'author'), 'taxonomies' => array( 'categories' ), ) ); } );
已尝试更新固定链接,问题未解决。
解决方案
问题根源在于自定义分类法的slug使用了categories,和WordPress默认分类法(slug为category)的复数形式命名冲突,导致Gutenberg编辑器(基于REST API驱动)识别混淆,进而在编辑页面加载了默认分类。
具体修复步骤
- 修改自定义分类法的slug:将
categories改为唯一名称(比如blog_categories),彻底避免和默认分类法的命名冲突。 - 同步文章类型关联的分类法:注册自定义文章类型时,引用修改后的分类法slug。
- 更新固定链接并清理缓存:修改代码后,进入WordPress后台「设置-固定链接」页面直接保存(无需修改设置),同时清理WordPress缓存和浏览器缓存。
修改后的完整代码:
add_action( 'init', function() { // 修改分类法slug为blog_categories register_taxonomy( 'blog_categories', array( 'blog' ), [ 'labels' => [ 'name' => 'Blog Categories', 'singular_name' => 'Blog Category', ], 'public' => true, 'hierarchical' => true, 'show_in_rest' => true, 'show_admin_column' => true, ] ); register_post_type( 'blog', array( 'labels' => array( 'name' => 'Blog posts', 'singular_name' => 'Blog post' ), 'public' => true, 'has_archive' => true, 'show_in_rest' => true, 'supports' => array('title','editor', 'thumbnail', 'author'), // 引用修改后的分类法slug 'taxonomies' => array( 'blog_categories' ), ) ); } );
数据迁移说明
如果已经创建了旧的分类数据,需要通过数据库工具(如phpMyAdmin),将wp_terms、wp_term_taxonomy、wp_term_relationships表中相关的taxonomy字段值从categories批量替换为blog_categories,避免数据丢失。
内容的提问来源于stack exchange,提问作者Ivan
相关产品推荐
相关产品推荐

