0

I need a function to paste in my theme that automatically rename the base category permalink from category to genre after activating the theme, example:

http://localhost/category/drama/

to

http://localhost/genre/drama/
1

So you can quickly achieve this by changing 'category' to 'genre' you can do this in Settings > Permalinks and change 'Category Base' to 'genre'.

To do it with code and keep both 'category' and 'genre' as prefixes is possible with a rewrite rule, however someone else may know a better way. You also need to only run flush_rewrite_rules() once so you need to take care of only calling this code when you change the rewrite rule!

This works for me in functions.php:

function add_category_base() {
  add_rewrite_rule('^genre/([^/]+)/?', 'index.php?taxonomy=category&term=$matches[1]', 'top');
  flush_rewrite_rules();
}

add_action('init', 'add_category_base', 10, 0);

As I said, you should not run flush_rewrite_rules all the time as it does a lot of stuff and only needs to be run when you change the rule.

2
  • it works but it doesn't change the base of the categories in wp list categories function :( Jul 1 '20 at 16:58
  • For me the URL's from wp_list_categories look like /?cat=2 . Which isn't related to the string 'category' in the URL. You may have to do more complicated stuff if you want to change that
    – mozboz
    Jul 1 '20 at 17:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.