admin管理员组文章数量:1306623
I have a 'resources' CPT with 'resources_category' (custom category) and 'resources_term' (custom terms). They are all working, however the category and terms URL's pick up the base permalink slug ('news') which is used for the blog posts.
ie. the following 2 URL's are working
/resources/
/resources/post-title/
however the taxonomy URL's are
/news/resources/category/videos/
/news/resources/tags/water/
I wish to change these to (remove the 'news')
/resources/category/videos/
/resources/tags/water/
Changing
'rewrite' => array( 'slug' => 'resources/tags', 'with_front' => true ),
to
'rewrite' => array( 'slug' => 'resources/tags', 'with_front' => false ),
returns a 404 error.
Full code as follows:
register_taxonomy( 'resource_category', array( 'resources' ), array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Resource Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Resource Category', 'taxonomy singular name' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'resources/category', 'with_front' => true ),
) );
register_taxonomy( 'resource_tag', 'resources', array(
'hierarchical' => false,
'labels' => array(
'name' => _x( 'Resource Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Resource Tag', 'taxonomy singular name' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'resources/tags', 'with_front' => true ),
) );
register_post_type( 'resources', array(
'labels' => array(
'name' => __( 'Resources' ),
'singular_name' => __( 'Resource' ),
),
'public' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-media-document',
'rewrite' => array( 'slug' => 'resources', 'with_front' => false ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' ),
'taxonomies' => array('resource_category', 'resource_tag'),
) );
With permalink setting structure
/news/%postname%/
I have a 'resources' CPT with 'resources_category' (custom category) and 'resources_term' (custom terms). They are all working, however the category and terms URL's pick up the base permalink slug ('news') which is used for the blog posts.
ie. the following 2 URL's are working
/resources/
/resources/post-title/
however the taxonomy URL's are
/news/resources/category/videos/
/news/resources/tags/water/
I wish to change these to (remove the 'news')
/resources/category/videos/
/resources/tags/water/
Changing
'rewrite' => array( 'slug' => 'resources/tags', 'with_front' => true ),
to
'rewrite' => array( 'slug' => 'resources/tags', 'with_front' => false ),
returns a 404 error.
Full code as follows:
register_taxonomy( 'resource_category', array( 'resources' ), array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Resource Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Resource Category', 'taxonomy singular name' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'resources/category', 'with_front' => true ),
) );
register_taxonomy( 'resource_tag', 'resources', array(
'hierarchical' => false,
'labels' => array(
'name' => _x( 'Resource Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Resource Tag', 'taxonomy singular name' ),
),
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'resources/tags', 'with_front' => true ),
) );
register_post_type( 'resources', array(
'labels' => array(
'name' => __( 'Resources' ),
'singular_name' => __( 'Resource' ),
),
'public' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-media-document',
'rewrite' => array( 'slug' => 'resources', 'with_front' => false ),
'query_var' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'revisions' ),
'taxonomies' => array('resource_category', 'resource_tag'),
) );
With permalink setting structure
/news/%postname%/
- Did you resave/flush your permalinks after making the change? – Tom J Nowell ♦ Commented Jan 18, 2021 at 9:52
- Yes, sure did... – christiaan Commented Jan 18, 2021 at 21:42
1 Answer
Reset to default 0via: https://cnpagency/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/
I needed to change the order of registering the taxonomy (make sure it's before CPT) and the CPT (make sure it's after registering any taxonomy).
Now
'rewrite' => array( 'slug' => 'resources/tags', 'with_front' => false ),
works as expected!
本文标签: Remove custom permalink base from CPT custom category and custom tags
版权声明:本文标题:Remove custom permalink base from CPT custom category and custom tags 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741823892a2399531.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论