How to remove /category/ from URL (wordpress + polylang)

132 views Asked by At

I am using the Polylang plugin and want to remove /category/ from the URL.

Before I installed the plugin, I used this:

add_filter('category_link', function($a){ return str_replace( 'category/', '', $a ); }, 99 );

It works for the main language, but it doesn't work for language versions. For example:

site.com/blog/ - works, response code 200
site.com/de/blog/ - not working, response code 404
site.com/de/category/blog/ - works, response code 200

I want both pages (site.com/blog/ and site.com/de/blog/) to work. How do I do that?

1

There are 1 answers

0
TSCAmerica.com On BEST ANSWER

You'll need to adjust your filter to account for the language prefix. Here's an example of how you might modify your filter to work with Polylan like the following

add_filter('category_link', function($a){
    $current_language = pll_current_language();

    if ($current_language && $current_language !== pll_default_language()) {
        return str_replace('/category/', '/' . $current_language . '/', $a);
    } else {
        // If it's the default language, just remove 'category/'
        return str_replace('/category/', '/', $a);
    }
}, 99);