Suppose you are using WordPress multisite setup for multilingual websites then you may need to change the slug of the same custom post type according to website language. In this example, we will discuss a similar situation and change the slugs for post type and taxonomy according to it.
Multisite setup for this example
Site 1 (Italian) – https://example.com
Site 2 (English) – https://example.com/en/
Now suppose we have defined a new custom post-type servizi in the child theme and defined its slug as servizi. Now it’s fine as long as we are viewing the custom post type content from the Italian version of the website but the problem occurs when we switch to the English version of the website. It will still show servizi as the slug of the custom post. To solve this problem we have written this small code snippet based on the register_post_type_args WordPress hook. It will check for the blog ID in the multisite network and update the custom post type slug according to the given condition. In our case, it will update the slug from servizi to services when the blog ID is 2.
function rp_change_custom_post_types_slug( $args, $post_type ) {
$site = get_current_blog_id();
if ($site == 2 && 'servizi' === $post_type ) {
$args['rewrite']['slug'] = 'services';
}
return $args;
}
add_filter( 'register_post_type_args', 'rp_change_custom_post_types_slug', 10, 2 );
Similarly, we can use the register_taxonomy_args WordPress hook to change the custom taxonomy slug.
function rp_change_taxonomies_slug( $args, $taxonomy ) {
$site = get_current_blog_id();
if ($site == 2 && 'servizi-categories' === $taxonomy ) {
$args['rewrite']['slug'] = 'services-categories';
}
return $args;
}
add_filter( 'register_taxonomy_args', 'rp_change_taxonomies_slug', 10, 2 );
Note – Don’t forget to update the permalinks after adding this code snippet
Whether you're facing website issues or struggling with code implementation, our team is here to assist. Hire us to get your website back on track.
Hire Us