Reputation: 11
does anyone have an easy solution to remove the "product-category" slug in my urls?
For examples I want: www.website.com/product-category/table
to be www.website.com/table
I've tried: Settings -> Permalinks -> Remove productcategory base. But that doesn't work.
I prefer not to use any plugin
Upvotes: 0
Views: 830
Reputation: 61
You can do it easily by adding some custom code to your WordPress theme's functions.php file. This solution involves using a custom rewrite rule to achieve the desired URL structure.
function custom_rewrite_rules() {
add_rewrite_rule(
'(.+?)/?$',
'index.php?product_cat=$matches[1]',
'top'
);
}
add_action('init', 'custom_rewrite_rules', 10, 0);
function change_product_cat_link($termlink, $term, $taxonomy) {
if ($taxonomy == 'product_cat') {
$termlink = str_replace('/product-category/', '/', $termlink);
}
return $termlink;
}
add_filter('term_link', 'change_product_cat_link', 10, 3);
This code adds a custom rewrite rule to map the new URLs to the correct product categories and changes the links generated for product categories.
Note: After adding code to ensure the changes take effect, you need to flush your permalinks. To do this, go to "Settings" > "Permalinks" in your WordPress dashboard and simply click the "Save Changes" button without making any modifications. This will refresh your site's permalink structure.
Upvotes: 1