Change custom taxonomy archive permalink

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x