2

In permalinks settings the 'Shop Base with Category' /shop/%product_cat%/ permalink setting puts all the hierarchical categories in the url, for example:

http:// mystore.com/shop/parent-category/child-category/product

I want to remove anything between parent-category and product so my urls look like:

http:// mystore.com/shop/parent-category/product

I am pretty sure that setting up urls like this does not cause problems, because I can manually type the shortened url into an address bar and it goes to the same place as the longer one.

Thanks.

1
  • This is something I have just found too, but it doesn't work if the URL is already in the browser - ie, linked externally. If Google has indexed it with the Sub Category version, can a function strip out the sub-category, and 301 it to the version asked about here?
    – Simon_a6
    Commented Nov 17, 2023 at 14:13

1 Answer 1

5

It's actually quite simple. Use the woocommerce_product_post_type_link_parent_category_only filter:

add_filter( 'woocommerce_product_post_type_link_parent_category_only', '__return_true' );

Tried and tested working.

PS: The code would go into the theme functions file and __return_true() is a WordPress function.

3
  • I had been working on custom rewrites. This works perfectly, thanks.
    – AJD
    Commented May 24, 2019 at 14:53
  • You're welcome. :)
    – Sally CJ
    Commented May 24, 2019 at 15:10
  • THANK YOU THANK YOU. Worked for me after 1,5 weeks tring with plugins, etc,...
    – Slavko
    Commented Feb 16, 2022 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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