admin管理员组

文章数量:1122832

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/shop/parent-category/child-category/product

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

http:// mystore/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.

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.

Share Improve this question asked May 24, 2019 at 0:12 AJDAJD 3492 gold badges4 silver badges14 bronze badges 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
Add a comment  | 

2 Answers 2

Reset to default 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.

If you just want to remove the immediate parent category slug from the URL (or any number of ancestors), there's another useful filter in wc-product-functions.php, several lines before the woocommerce_product_post_type_link_parent_category_only filter:

// Remove one or more parent categories from Woo product URL
function my_product_post_type_link_product_cat( $parent_term, $terms, $post ) {
    // Choose which products to modify
    if ( has_term('some-child-category', 'product_cat', $post ) ) {
        // Term ID of desired final category slug in URL
        $parent_term = get_term(123);
    } else if ( $post->ID == 2962 ) { // Target single product
        $parent_term = get_term(321);
    }
    return $parent_term;
}
add_filter( 'wc_product_post_type_link_product_cat', 'my_product_post_type_link_product_cat', 10, 3 );

The $permalink generated by the wc_product_post_type_link() function (which contains the aforementioned two filters) is then passed to the post_type_link filter if you prefer to take the hammer approach (for example, using str_replace).

When choosing which products to modify, avoid conditionals like is_product_category, otherwise the URL won't be modified in other places (like search results). Instead make sure to use functions that reference the ubiquitous $post object.

本文标签: permalinksHow to remove the subcategory from Woocommerce product URL