【问题标题】:Woocommerce rewrite rule for product sub category产品子类别的 Woocommerce 重写规则
【发布时间】:2017-06-25 21:34:29
【问题描述】:

在此处使用此代码:

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;
});

并且拥有这些永久链接设置

产品类别库: products
产品永久链接 - 自定义库: /products/%product_cat%

我得到以下信息:

  • /products/%category_name%/(父产品类别页面工作正常)。
  • /products/%category_name%/%subcategory_name/(子产品类别页面不起作用 - 得到 404)。
  • /products/%category_name%/%subcategory_name/%product_name(单一产品工作正常)。

有人对如何让子产品类别页面不抛出 404 错误有任何见解吗?

【问题讨论】:

    标签: php wordpress url url-rewriting woocommerce


    【解决方案1】:

    我解决了这些问题,我们应该对上面的代码做一些小改动。

    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);

    它非常适合我。

    【讨论】:

    • 感谢您回答这个问题...在 WP 4.9.6、WooCom 3.4.3 上对我不起作用。根据 OP Child Product Cat 返回 404。我想知道我遗漏了什么,我应该用自己的文本替换 吗?
    • 啊,我意识到我还有一个更复杂的问题,因为我的 Shop url(“Store”)也是一个名为 Products 的页面。也许我会开始一个新的线程。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2017-06-04
    • 2018-05-17
    • 1970-01-01
    相关资源
    最近更新 更多