admin管理员组

文章数量:1278976

Suppose the following categories tree in Woo:

Men (slug men)
-- Shoes (slug shoes / was created first)
Women
-- Shoes (slug shoes-women / was created second)

Why is this happening? Both subcats belong to different categories. Why is the category slug appended to the name of the second duplicate subcat?

And anyway, for whatever reason Autommatic decided to program it that way, I need to change it, as the nature of the e-shop I'm building is full of such duplicate subcat names, as it will sell clothing, and there will be the same subcat and even subsubcat names for many categories (Men, Women, Kids, etc)...

Any insight on this?

Suppose the following categories tree in Woo:

Men (slug men)
-- Shoes (slug shoes / was created first)
Women
-- Shoes (slug shoes-women / was created second)

Why is this happening? Both subcats belong to different categories. Why is the category slug appended to the name of the second duplicate subcat?

And anyway, for whatever reason Autommatic decided to program it that way, I need to change it, as the nature of the e-shop I'm building is full of such duplicate subcat names, as it will sell clothing, and there will be the same subcat and even subsubcat names for many categories (Men, Women, Kids, etc)...

Any insight on this?

Share Improve this question asked Oct 30, 2021 at 2:12 Faye D.Faye D. 1266 bronze badges 1
  • 1 (Congrats on asking a WooCommerce question that's actually about core WP function, BTW - almost nobody manages that. Unless it turns out it is Woo that's breaking this!) – Rup Commented Nov 1, 2021 at 10:59
Add a comment  | 

1 Answer 1

Reset to default 0

It took me two days to figure this out in the proper way, so I'll add it here for anyone else that may need it in the future.

add_filter('wp_unique_term_slug', 'prevent_cat_suffix', 10, 3);
function prevent_cat_suffix($slug, $term, $original_slug)
{
    if ($term->post_type == 'product' && $term->parent !== 0) {
        return $original_slug;
    }

    return $slug;
}

As you can see in the code, I've narrowed it down to products only, but you can make it suitable for any other post type also by removing the $term->post_type == 'product' && in the code above.

本文标签: