最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove custom permalink base from CPT custom category and custom tags

programmeradmin1浏览0评论

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%/

Share Improve this question edited Jan 18, 2021 at 22:33 christiaan asked Jan 18, 2021 at 9:01 christiaanchristiaan 11 bronze badge 2
  • 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
Add a comment  | 

1 Answer 1

Reset to default 0

via: 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!

发布评论

评论列表(0)

  1. 暂无评论