0

I have created a custom post type named video and a custom taxonomy for video as video_category. I have created a page template with the name taxonomy-video_category.php. So that the categories of the videos can be viewed on the URL mysite.com/video_category/{category_name}.

I need to change this URL to mysite.com/videos/category/{category_name}. I tried the plugin Custom Permalinks. But it does not allow me to change this URL on edit of the taxonomy video_category.

I want to do this preferably without the use of plugins. How do I do this?

1

You can use the rewrite parameter to customize the taxonomy permalinks.

Here's an example which links the taxonomy to the default post post type:

register_taxonomy( 'video_category', 'post', [
    'public'   => true,
    'rewrite'  => [
        'slug' => 'videos/category',
    ],
    // ... your other parameters ..
] );

Don't forget to flush the rewrite rules — just visit the permalink settings admin page.

And if the taxonomy is being registered by a plugin and (just in case) it doesn't allow changing the rewrite slug, there's a filter hook you can use to change the slug programmatically: register_taxonomy_args. Here's a simplified example:

add_filter( 'register_taxonomy_args', 'my_register_taxonomy_args', 10, 2 );
function my_register_taxonomy_args( $args, $taxonomy ) {
    if ( 'video_category' === $taxonomy ) {
        $args['rewrite'] = (array) $args['rewrite'];
        $args['rewrite']['slug'] = 'videos/category';
    }

    return $args;
}
2
  • Thanks. Will this change the slug overall like when using get_terms() or just in the URL? – Saeesh Tendulkar Oct 9 '20 at 12:15
  • It will change both, I mean when using get_term_link() and functions using it - that's how it works when the permalink structure is customized via that rewrite param. But you can try and see for yourself. :) – Sally CJ Oct 9 '20 at 12:50

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.