删除woocommerce重复分类页面链接?

时间:2017-06-03 17:52:16

标签: woocommerce duplicates taxonomy

我遇到了重复内容的问题,这似乎与woocommerce有一个开箱即用的问题。例如,我们的SEO公司因为重复内容而抱怨,因为您可以访问:

http://localhost/wp7/product-category/clothing/hoodies/

但你也可以去

http://localhost/wp7/product-category/hoodies/

所以clothinghoodies的父分类。是否有过滤器/代码,以便您无法导航到子分类链接,但只允许http://www.example.com/product-category/parent/child

对此的任何帮助将不胜感激。

亲切的问候,谢谢!

1 个答案:

答案 0 :(得分:0)

通过这个解决方案,我能够解决这个问题。这看起来很漂亮。不确定woocommerce生态系统的影响,但它绝对适用于前端。

/**
 * Disallow duplicate Woocommerce taxonomy links
 * |- Redirects http://www.example.com/product_cat/child to http://www.example.com/product_cat/parent/child
 */
add_action('template_redirect', function ($query) {
    global $wp;
    $productCatTaxonomy = 'product_cat';

    if (is_tax($productCatTaxonomy)) {
        $current_url = add_query_arg($wp->query_string, '', home_url($wp->request));
        $current_args = parse_url($current_url);
        $currentTerm = get_term_by('slug', get_query_var('term'), $productCatTaxonomy);

        if (isset($current_args['query']) && !stristr($current_args['query'], '%2f')) {
            $currentParentTerm = (isset($currentTerm->parent) && $currentTerm->term_id) ? get_term_by('id', $currentTerm->parent, $productCatTaxonomy) : 0;

            if ($currentParentTerm) {
                $childTermLink = (isset($currentTerm->term_id) && $currentTerm->term_id) ? get_term_link($currentTerm->term_id, $productCatTaxonomy) : 0;

                if ($childTermLink) {
                    wp_redirect($childTermLink);
                    die();
                }
            }
        }
    }

    return $query;
});
相关问题