Hello,
This is not that simple… In order to do this with multilanguage you need to add rewrite rules for every translation of every categories. This snippet should do it:
add_filter('category_rewrite_rules', function($rules) {
global $wp_rewrite, $sublanguage_admin;
if (isset($sublanguage_admin)) {
$terms = get_terms(array(
'taxonomy' => 'category',
'hide_empty' => false
));
$term_slugs = array();
foreach ($sublanguage_admin->get_languages() as $language) {
if ($sublanguage_admin->is_sub($language)) {
$taxonomy_slug = $sublanguage_admin->translate_taxonomy('category', $language, 'category');
foreach ($terms as $term) {
$term_slugs[] = $sublanguage_admin->translate_term_field($term, 'category', 'slug', $language, $term->slug);
}
$term_slug_group = implode('|', $term_slugs);
$rules["({$term_slug_group})/{$wp_rewrite->pagination_base}/?([0-9]{1,})/?$"] = 'index.php?sublanguage_slug='.$taxonomy_slug.'&category_name=$matches[1]&paged=$matches[2]';
$rules['('.$term_slug_group.')/?$'] = 'index.php?sublanguage_slug='.$taxonomy_slug.'&category_name=$matches[1]';
}
}
}
return $rules;
}, 11);
Then you can fix the category links this way:
add_filter('term_link', function($termlink, $term, $taxonomy) {
global $wp_rewrite, $sublanguage;
$permastruct = $wp_rewrite->get_extra_permastruct($taxonomy);
if (isset($sublanguage) && $permastruct) {
$translated_tax = $sublanguage->translate_taxonomy($taxonomy, null, $taxonomy);
$translated_slug = $sublanguage->translate_term_field($term, $taxonomy, 'slug');
$termlink = str_replace(array($taxonomy, "%$translated_tax%"), array($translated_tax, $translated_slug), $permastruct);
$termlink = home_url(user_trailingslashit($termlink, 'category'));
}
return $termlink;
}, 11, 3);
Tell me if it works. Don’t forget to refresh your permalinks!
Hi Maxime,
thank you very much!
The above snippets work perfectly in combination with the Remove Category URL plugin.
Thanks again for the fantastic support!