WordPress自定义分类法与自定义文章类型URL结构404问题求助
WordPress自定义文章类型与分类法URL 404错误排查与修复
核心问题梳理
你的代码存在多处配置不匹配、规则缺失的问题,直接导致URL重写失效,触发404错误:
1. 分类法与文章类型绑定错误
注册category和part分类法时,关联的文章类型写的是template,但实际注册的自定义文章类型是theme,两者完全不匹配,导致分类法无法和目标文章类型建立关联,重写规则无法生成。
2. 固定结构(Permastruct)配置错误
- 多余的
product固定结构配置:代码开头的add_permastruct('product', ...)没有对应的文章类型/分类法,会干扰整体重写规则。 - 占位符无替换逻辑:
part和文章类型的URL中使用了%category%占位符,但没有编写回调函数将其替换为实际的分类slug,WordPress无法解析动态URL。 - 占位符名称不匹配:自定义文章类型
theme的rewrite参数用了%categories%(复数),但注册的分类法是category(单数),规则无法匹配。
3. 分类法重写规则被禁用
两个分类法注册时设置了rewrite => false,直接禁用了分类法的URL重写功能,导致分类归档页面无法通过URL访问。
分步修复方案
1. 修正分类法与文章类型的绑定
将两个分类法的关联文章类型从template改为theme:
function cptui_register_my_taxes_category() { $labels = array( "name" => __( "Categories", "my-theme" ), "singular_name" => __( "Category", "my-theme" ), ); $args = array( "label" => __( "Categories", "my-theme" ), "labels" => $labels, "public" => true, "publicly_queryable" => true, "hierarchical" => true, "show_ui" => true, "show_in_menu" => true, "show_in_nav_menus" => false, "query_var" => true, "rewrite" => array( 'slug' => 'category', 'with_front' => false ), // 启用重写 "show_admin_column" => true, "show_in_rest" => true, "rest_base" => "category", "rest_controller_class" => "WP_REST_Terms_Controller", "show_in_quick_edit" => true, ); register_taxonomy( "category", array( "theme" ), $args ); // 改为关联theme } add_action( 'init', 'cptui_register_my_taxes_category' ); function cptui_register_my_taxes_template_type() { $labels = array( "name" => __( "Parts", "my-theme" ), "singular_name" => __( "Part", "my-theme" ), "edit_item" => __( "Edit Part", "my-theme" ), ); $args = array( "label" => __( "Parts", "my-theme" ), "labels" => $labels, "public" => true, "publicly_queryable" => true, "hierarchical" => true, "show_ui" => true, "show_in_menu" => true, "show_in_nav_menus" => true, "query_var" => true, "rewrite" => array( 'slug' => 'part', 'with_front' => false ), // 启用重写 "show_admin_column" => true, "show_in_rest" => true, "rest_base" => "part", "rest_controller_class" => "WP_REST_Terms_Controller", "show_in_quick_edit" => true, ); register_taxonomy( "part", array( "theme" ), $args ); // 改为关联theme } add_action( 'init', 'cptui_register_my_taxes_template_type' );
2. 修复固定结构与占位符替换
- 移除多余的
product配置,修正固定结构; - 添加回调函数替换URL中的
%category%占位符:
// 配置category分类法固定结构 add_permastruct('category', '/%category%/', array( 'with_front' => false )); // 配置part分类法固定结构 add_permastruct('part', '/%category%/themes/%part%/', array( 'with_front' => false )); // 替换文章URL中的%category%占位符 function replace_category_permalink_placeholder($post_link, $post) { if ($post->post_type === 'theme') { $terms = wp_get_object_terms($post->ID, 'category'); if (!empty($terms) && !is_wp_error($terms)) { return str_replace('%category%', $terms[0]->slug, $post_link); } } return $post_link; } add_filter('post_type_link', 'replace_category_permalink_placeholder', 10, 2);
3. 修正自定义文章类型的rewrite配置
将theme文章类型的rewrite参数中的%categories%改为%category%,并去掉开头的斜杠:
function cptui_register_my_cpts_themes() { $labels = array( "name" => __( "Themes", "my-theme" ), "singular_name" => __( "Theme", "my-theme" ), "menu_name" => __( "Themes", "my-theme" ), "all_items" => __( "All Themes", "my-theme" ), "add_new_item" => __( "Add New Theme", "my-theme" ), "edit_item" => __( "Edit Theme", "my-theme" ), "new_item" => __( "New Theme", "my-theme" ), "view_item" => __( "View Theme", "my-theme" ), "view_items" => __( "View Themes", "my-theme" ), "search_items" => __( "Search Theme", "my-theme" ), "not_found" => __( "No templates found", "my-theme" ), ); $args = array( "label" => __( "Themes", "my-theme" ), "labels" => $labels, "description" => "", "public" => true, "publicly_queryable" => true, "show_ui" => true, "delete_with_user" => false, "show_in_rest" => true, "rest_base" => "", "rest_controller_class" => "WP_REST_Posts_Controller", "has_archive" => true, "show_in_menu" => true, "show_in_nav_menus" => true, "exclude_from_search" => false, "capability_type" => "post", "map_meta_cap" => true, "hierarchical" => false, "rewrite" => array( "slug" => "%category%/themes", "with_front" => false ), // 修正占位符与路径 "query_var" => true, "menu_icon" => "/wp-content/uploads/2023/02/templates.webp", "supports" => array( "title", "thumbnail" ), "taxonomies" => array( "category","part" ), ); register_post_type( "theme", $args ); } add_action( 'init', 'cptui_register_my_cpts_themes' );
4. 刷新重写规则
完成代码修改后,必须登录WordPress后台,进入设置→固定链接,直接点击保存更改(无需修改任何设置),触发重写规则刷新,这是让新URL规则生效的关键步骤。
额外注意事项
- 确保每个
theme文章都至少分配了一个category分类,否则%category%占位符无法被替换,仍会触发404; - 如果使用多层分类(父分类+子分类),可调整替换函数,将
$terms[0]->slug改为$terms[0]->parent ? get_term($terms[0]->parent)->slug . '/' . $terms[0]->slug : $terms[0]->slug,以支持层级分类路径。
内容的提问来源于stack exchange,提问作者Christian
相关产品推荐
相关产品推荐

