ACF插件迁移CPT UI后分类法页面无法访问求助
从CPT UI迁移到ACF后自定义分类法页面无法访问的解决方法
之前使用Custom Post Type UI插件时,链接结构site.com/portfolio/、site.com/portfolio/category/uncategorized/均能正常访问。通过ACF的迁移功能将CPT UI的配置迁移过来后,出现自定义分类法页面无法访问的问题,以下是ACF生成的代码:
add_action( 'init', function() { register_post_type( 'portfolio', array( 'labels' => array( 'name' => 'portfolio', 'singular_name' => 'portfolio', ), 'public' => true, 'show_in_rest' => true, 'supports' => array( 0 => 'title', 1 => 'editor', 2 => 'excerpt', 3 => 'revisions', 4 => 'thumbnail', 5 => 'custom-fields', ), 'taxonomies' => array( 0 => 'portfolio-category', ), 'has_archive' => true, 'rewrite' => array( 'with_front' => false, 'feeds' => false, ), 'can_export' => false, 'delete_with_user' => false, ) ); } ); add_action( 'init', function() { register_taxonomy( 'portfolio-category', array( 0 => 'portfolio', ), array( 'labels' => array( 'name' => 'category', 'singular_name' => 'category', ), 'public' => true, 'hierarchical' => true, 'show_in_menu' => true, 'show_in_rest' => true, 'show_tagcloud' => false, 'show_in_quick_edit' => false, 'rewrite' => array( 'slug' => 'portfolio/category', 'with_front' => false, 'hierarchical' => true, ), ) ); } );
问题原因
- 注册顺序错误:自定义分类法
portfolio-category的注册时机晚于自定义文章类型portfolio,但注册CPT时指定了taxonomies参数,导致WordPress无法识别关联的分类法。 - 重写规则冲突:分类法的重写slug设置为
portfolio/category,与CPT的存档路径portfolio重叠,引发URL解析混乱。
修复方案
调整注册顺序,确保分类法先注册,同时优化配置以匹配原有URL结构:
// 先注册自定义分类法,设置优先级9确保早于CPT注册 add_action( 'init', function() { register_taxonomy( 'portfolio-category', array( 'portfolio' ), array( 'labels' => array( 'name' => '分类', 'singular_name' => '分类', ), 'public' => true, 'hierarchical' => true, 'show_in_menu' => true, 'show_in_rest' => true, 'show_tagcloud' => false, 'show_in_quick_edit' => false, 'rewrite' => array( 'slug' => 'portfolio/category', 'with_front' => false, 'hierarchical' => true, ), ) ); }, 9 ); // 后注册自定义文章类型,默认优先级10 add_action( 'init', function() { register_post_type( 'portfolio', array( 'labels' => array( 'name' => '作品集', 'singular_name' => '作品集', ), 'public' => true, 'show_in_rest' => true, 'supports' => array( 'title', 'editor', 'excerpt', 'revisions', 'thumbnail', 'custom-fields' ), 'taxonomies' => array( 'portfolio-category' ), 'has_archive' => true, 'rewrite' => array( 'with_front' => false, 'feeds' => false, ), 'can_export' => false, 'delete_with_user' => false, ) ); }, 10 );
后续操作
替换ACF生成的代码后,进入WordPress后台设置 → 固定链接页面,直接点击「保存更改」按钮(无需修改任何设置),刷新WordPress的重写规则,确保新的URL结构生效。
内容的提问来源于stack exchange,提问作者widkoinflm
相关产品推荐
相关产品推荐

