自定义文章类型(CPT)自定义URL结构失效求助
解决WordPress自定义URL结构
custompostype/taxonomy/singlepost的404问题 我明白你想要实现/myposttype/customtaxonomy/post-slug这种嵌套式的单篇文章URL,但当前访问时出现404的困扰——这种情况确实是因为WordPress默认的rewrite规则无法识别这种自定义的嵌套结构,咱们一步步来解决:
1. 先做最基础但容易忽略的操作:刷新固定链接
不管你怎么修改代码,最后都需要让WordPress重新生成rewrite规则:
- 登录WordPress后台,进入设置 → 固定链接
- 随便切换一种规则(比如从「文章名」改成「朴素」),点击保存
- 再切回你原来使用的固定链接规则,再次保存
这一步能确保新的rewrite规则被写入.htaccess(Apache环境)或者服务器配置(Nginx环境)。
2. 调整自定义文章类型的Rewrite配置
你的自定义文章类型myposttype的rewrite参数需要明确指定slug,确保和分类的slug结构匹配:
function learning() { $labels = array( 'name' => _x( 'Myposttype', 'Post Type General Name', 'text_domain' ), 'singular_name' => _x( 'Myposttype', 'Post Type Singular Name', 'text_domain' ), 'menu_name' => __( 'Myposttype', 'text_domain' ), 'name_admin_bar' => __( 'Myposttype', 'text_domain' ), 'archives' => __( 'Archiwa', 'text_domain' ), 'attributes' => __( 'Atrybuty', 'text_domain' ), 'parent_item_colon' => __( 'Parent Item:', 'text_domain' ), 'all_items' => __( 'Wszystkie', 'text_domain' ), 'add_new_item' => __( 'Dodaj nowy post', 'text_domain' ), 'add_new' => __( 'Dodaj nowy', 'text_domain' ), 'new_item' => __( 'Nowy', 'text_domain' ), 'edit_item' => __( 'Edytuj', 'text_domain' ), 'update_item' => __( 'Zaktualizuj', 'text_domain' ), 'view_item' => __( 'Zobacz', 'text_domain' ), 'view_items' => __( 'Zobacz', 'text_domain' ), 'search_items' => __( 'Szukaj', 'text_domain' ), 'featured_image' => __( 'Obrazek wyróżniający', 'text_domain' ), 'set_featured_image' => __( 'Ustaw obrazek wyróżniający', 'text_domain' ), 'remove_featured_image' => __( 'Usuń obrazek', 'text_domain' ), ); $args = array( 'label' => __( 'Myposttype', 'text_domain' ), 'labels' => $labels, 'supports' => array( 'title', 'editor', 'thumbnail', 'page-attributes' ), 'taxonomies' => array( 'customtaxonomy'), 'hierarchical' => true, 'public' => true, 'show_ui' => true, 'show_in_menu' => true, 'menu_position' => 5, 'menu_icon' => 'dashicons-format-chat', 'show_in_admin_bar' => true, 'show_in_nav_menus' => true, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post', // 修改这里的rewrite参数 'rewrite' => array( 'slug' => 'myposttype', 'with_front' => false, 'pages' => true, 'feeds' => true ), ); register_post_type( 'myposttype', $args ); } add_action( 'init', 'learning', 0 );
3. 添加自定义Rewrite规则和查询处理
因为WordPress默认不会解析/myposttype/{tax-term}/{post-slug}这种结构,所以需要手动添加规则告诉它这个URL对应的是myposttype的单篇文章:
// 添加自定义Rewrite规则 function custom_posttype_taxonomy_rewrite_rule() { add_rewrite_rule( '^myposttype/([^/]+)/([^/]+)/?$', 'index.php?post_type=myposttype&name=$matches[2]&customtaxonomy=$matches[1]', 'top' ); } add_action('init', 'custom_posttype_taxonomy_rewrite_rule'); // 确保查询变量能被识别 function custom_posttype_taxonomy_query_var($vars) { $vars[] = 'customtaxonomy'; return $vars; } add_filter('query_vars', 'custom_posttype_taxonomy_query_var');
这段代码的作用是:
- 匹配
/myposttype/分类术语/文章slug的URL格式 - 告诉WordPress去查询
myposttype类型中slug为$matches[2]的文章,同时关联customtaxonomy的术语$matches[1] - 把
customtaxonomy加入到允许的查询变量中,避免被WordPress过滤
4. 确认文章与分类术语的关联
最后要检查一下你访问的那篇文章,是否真的属于你URL中指定的customtaxonomy术语——如果文章没有关联该术语,即使规则正确也会返回404。
完成以上步骤后,再次刷新固定链接设置,然后访问你的目标URL,应该就能正常显示文章内容了。
内容的提问来源于stack exchange,提问作者Natalia
相关产品推荐
相关产品推荐

