I want to add the slug of a custom taxonomy to the permalink of a WooCommerce product. To do this, I've added a taxonomy called brand. Now I want to add the slug of the brand to the product url.
Before: www.shop.com/product/product-name/
After: www.shop.com/product/brand-product-name/
I've done some research and found the following tutorial for something like that: https://wisdmlabs.com/blog/add-taxonomy-term-custom-post-permalinks-wordpress/
But I don't add the Custom Post Type "WooCommerce procuct" by myself so I don't know how to add the rewrite code from here:
'rewrite' => array('slug' => 'projects/%projectscategory%', 'with_front' => false),
EDIT: Here's my custom code:
add_filter('post_type_link', 'productbrand_permalink_structure', 10, 4);
function productbrand_permalink_structure($post_link, $post, $leavename, $sample) {
if (false !== strpos($post_link, '%brand%')) {
$productbrand_type_term = get_the_terms($post->ID, 'brand');
if (!empty($productbrand_type_term))
$post_link = str_replace('%brand%', array_pop($productbrand_type_term)->
slug, $post_link);
else
$post_link = str_replace('%brand%', 'uncategorized', $post_link);
}
return $post_link;
}
The problem is, that the URL looks like this:
www.shop.com/product/brand-/product-name/
Is there any way to remove the /
after the brand-
In my custom base inside the permalik option I added the following:
/product/%brand%-
But it always adds the /
to the end.
Actually it might be easier than you think.
Just visit Settings > Permalinks and scroll down to Product permalinks and set the Custom base with /product/%product_cat%/
and you're done.