将分类法​​术语添加到WooCommerce产品永久链接

时间:2018-05-25 07:49:50

标签: php wordpress url woocommerce permalinks

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

之前: 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%-

但它始终将/添加到最后。

1 个答案:

答案 0 :(得分:2)

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

只需访问设置>永久链接并向下滚动到产品永久链接,并将自定义基础设置为/product/%product_cat%/,您就完成了。

reigelgallarde.me

相关问题