【问题标题】:Add taxonomy Term to WooCommerce product permalink将分类术语添加到 WooCommerce 产品永久链接
【发布时间】:2018-11-04 13:15:48
【问题描述】:

我想将自定义分类法添加到 WooCommerce 产品的永久链接中。为此,我添加了一个名为品牌的分类法。现在我想将品牌的 slug 添加到产品 url。

之前: www.shop.com/product/product-name/

之后: www.shop.com/product/brand-product-name/

我做了一些研究,发现了以下类似的教程:https://wisdmlabs.com/blog/add-taxonomy-term-custom-post-permalinks-wordpress/

但我自己没有添加自定义帖子类型“WooCommerce procuct”,所以我不知道如何从这里添加重写代码:

'rewrite' => array('slug' => 'projects/%projectscategory%', 'with_front' => false),

编辑:这是我的自定义代码:

add_filter('post_type_link', 'productbrand_permalink_structure', 10, 4);
function productbrand_permalink_structure($post_link, $post, $leavename, $sample) {
    if (false !== strpos($post_link, '%brand%')) {
        $productbrand_type_term = get_the_terms($post->ID, 'brand');
        if (!empty($productbrand_type_term))
            $post_link = str_replace('%brand%', array_pop($productbrand_type_term)->
            slug, $post_link);
        else
            $post_link = str_replace('%brand%', 'uncategorized', $post_link);
    }
    return $post_link;
}

问题是,URL 看起来像这样:

www.shop.com/product/brand-/product-name/

有什么办法可以去掉brand-之后的/

在我的 permalik 选项内的自定义库中,我添加了以下内容:

/product/%brand%-

但它总是将/ 添加到末尾。

【问题讨论】:

    标签: php wordpress url woocommerce permalinks


    【解决方案1】:

    实际上可能比你想象的要容易。

    只需访问设置 > 永久链接并向下滚动到 产品永久链接 并使用 /product/%product_cat%/ 设置 自定义基础 即可。

    【讨论】:

    • 谢谢,但不是产品类别。这是一个自定义分类法
    • 那么我对你的第一个问题的回答'rewrite' => array('slug' => 'projects/%projectscategory%', 'with_front' => false),
    • 您可以按照您提供的链接上的说明进行操作。只需将重写部分放在 Custom base
    • 谢谢,我用我的自定义代码更新了我的问题。但是产品名前还有/
    • 使用 /product/%brand%/ 之类的东西,然后替换使用 $post_link = str_replace('%brand%/', array_pop($productbrand_type_term)-> slug . '-', $post_link);。不过我没试过
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2021-07-01
    • 2014-04-23
    • 2018-02-24
    • 2021-03-30
    相关资源
    最近更新 更多