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

2
  • Did you resave/flush your permalinks after making the change?
    – Tom J Nowell
    Jan 18 at 9:52
  • Yes, sure did...
    – christiaan
    Jan 18 at 21:42
0

via: https://cnpagency.com/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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.