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

Change custom taxonomy archive permalink

programmeradmin1浏览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/video_category/{category_name}.

I need to change this URL to mysite/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?

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/video_category/{category_name}.

I need to change this URL to mysite/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?

Share Improve this question asked Oct 9, 2020 at 9:38 Saeesh TendulkarSaeesh Tendulkar 1531 silver badge7 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

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;
}
发布评论

评论列表(0)

  1. 暂无评论