Woocommerce rewrite rule for product sub category

╄→尐↘猪︶ㄣ 提交于 2020-01-15 11:26:16

问题


Using this code here:

add_filter('rewrite_rules_array', function( $rules )
{
    $new_rules = array(
        'products/([^/]*?)/page/([0-9]{1,})/?$' => 'index.php?product_cat=$matches[1]&paged=$matches[2]',
        'products/([^/]*?)/?$' => 'index.php?product_cat=$matches[1]',
    );
    return $new_rules + $rules;
});

And having these permalink settings

Product category base: products
Product permalink - custom base: /products/%product_cat%

I get the following:

  • /products/%category_name%/ (Parent Product Category Page works fine).
  • /products/%category_name%/%subcategory_name/ (Child Product Category Page does not work - gets 404).
  • /products/%category_name%/%subcategory_name/%product_name (Single Products works fine).

Anybody have any insight on how to get the Child Product Category pages to not throw the 404 error?


回答1:


I fixed these issue we should do some little changes in above code.

add_filter( 'rewrite_rules_array', function( $rules )
{
    $new_rules = array(
        'products/([^/]+?)/?$' => 'index.php?product_cat=$matches[1]',
        'products(?:/.*)*?/(<!--some-indentword-->-[^/]+)/?$' => 'index.php?product_cat=$matches[1]',
    );
    return $new_rules + $rules;
} ,20,1);

It works perfectly for me.



来源:https://stackoverflow.com/questions/42110208/woocommerce-rewrite-rule-for-product-sub-category

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!