If you have decided to remove the ‘category’ prefix in the site url for category pages, you need to do the following steps:
- Go to the Wordpress Settings and leave a blank field in the Category base
Settings>Permalinks>Optional>Category base
If nothing changes after that, then try adding a snippet to the functions.php file of your child theme (or use a plugin to insert snippets).
// Remove the category base from the URL
add_filter('category_link', 'no_category_base', 1000, 2);
add_action('init', 'remove_category_base');
function no_category_base($catlink, $category) {
$catlink = str_replace('/category/', '/', $catlink);
return $catlink;
}
function remove_category_base() {
global $wp_rewrite;
$wp_rewrite->extra_permastructs['category'][0] = '%category%';
$wp_rewrite->flush_rules();
}
add_action('init', 'custom_rewrite_rules');
function custom_rewrite_rules() {
$categories = get_categories(array(
'taxonomy' => 'category',
'hide_empty' => false,
));
foreach ($categories as $category) {
$category_slug = $category->slug;
add_rewrite_rule("^{$category_slug}/?$", "index.php?category_name={$category_slug}", 'top');
}
}
After following these steps, the category pages on your WordPress site will work without the ‘category’ prefix.
Possible problems and solutions:
Nothing has changed. After all the changes, don’t forget to clean your site’s cache.
- Nothing has changed. After all the changes, don’t forget to clean your site’s cache.
- 404 errors on category pages. Go to permanent link settings and just click “Save Changes” to update the rewrite rules. Make sure you don’t have any conflicting plugins that can change the URL structure.
Leave a Reply