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

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驱动)识别混淆,进而在编辑页面加载了默认分类。

具体修复步骤

  1. 修改自定义分类法的slug:将categories改为唯一名称(比如blog_categories),彻底避免和默认分类法的命名冲突。
  2. 同步文章类型关联的分类法:注册自定义文章类型时,引用修改后的分类法slug。
  3. 更新固定链接并清理缓存:修改代码后,进入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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 06:10:35