【问题标题】:Breadcrumbs on product page don't reflect current url/slug when using multiple categories for product为产品使用多个类别时,产品页面上的面包屑不反映当前 url/slug
【发布时间】:2021-01-20 04:58:51
【问题描述】:

我的 Wordpress 网站使用 Woocommerce 来销售产品。但是,当您将多个产品类别分配给一个产品并访问产品页面(Product Permalinks 设置为Shop base with category)时,会发生一些意想不到的事情。指向(父)类别的面包屑链接始终是同一个,并且不反映导航的 url。 (这与here描述的问题相同。)

问题总结:

Navigate to Breadcrumb link Expected breadcrumb link
/shop/category-1/sample-product/ category-2 category-1
/shop/category-2/sample-product/ category-2 category-2

我在任何解决此问题的来源上都找不到真正的答案。 (我认为它应该被修复,但有些人可能会说这是出于 SEO 的原因,防止重复的网站内容。)所以为了帮助任何人寻找答案,这里是:

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce breadcrumbs


    【解决方案1】:

    (wr)on(g) 发生了什么?

    产品页面上的面包屑由woocommerce_breadcrumb() function 制作。这反过来又通过WC_Breadcrumb->generate()

    在代码中进一步遍历,您最终会进入 add_crumbs_single() 函数,其中 woocommerce_breadcrumb_product_terms_args 过滤器应用于产品术语获取的排序。 (有关更多信息,请参阅 wp_get_post_termsWP_Term_Query::__construct()。)

    从代码中可以清楚地看出,他们优先考虑具有最高 parent 值的词条,即数据库中最近添加父项的词条。

    解决方案 1

    由于此产品始终相同,因此您可能需要向主题的 function.php 添加过滤器(或使用自定义插件),以覆盖此行为。我用这个来工作:

    function prioritize_current_url_cat_in_breadcrumbs( $args ) {
        // Only if we're on a product page..
        if ( is_product() ) {
    
            // ..and there's a product category slug in the navigated url..
            $product_cat_slug = get_query_var( 'product_cat', '' );
            if ( ! empty($product_cat_slug) ) {
    
                // ..which we can find
                $product_cat = get_term_by( 'slug', $product_cat_slug, 'product_cat', ARRAY_A );
                if ( ! empty($product_cat) ) {
    
                    // Then only get that current product category to start the breadcrumb trail
                    $args['term_taxonomy_id'] = $product_cat['term_taxonomy_id'];
                }
            }
        }
        return $args;
    }
    add_filter( 'woocommerce_breadcrumb_product_terms_args', 'prioritize_current_url_cat_in_breadcrumbs' );
    

    add_crumbs_single() 函数的其余部分将负责遍历类别的父级等直到 Home。

    解决方案 2

    或者,您可以使用woocommerce_breadcrumb_main_term 过滤器来更改用于面包屑路径的“主要术语”。过滤器函数接收 2 个参数:主要术语(如 WP_Term)和一个包含找到所有产品类别的 WP_Terms 数组。它会返回一个术语,因此您可以在数组中搜索并选择您想要作为面包屑开头的正确术语。

    function prioritize_current_url_cat( $first_term, $all_terms ) {
        if ( is_product() ) {
            $product_cat_slug = get_query_var( 'product_cat', '' );
            if ( ! empty($product_cat_slug) ) {
                // Get the WP_Term with the current slug
                $filtered_terms = array_values( array_filter($all_terms, function($v) use ($product_cat_slug) {
                    return $v->slug === $product_cat_slug;
                }) );
                if ( ! empty($filtered_terms) ) {
                    return $filtered_terms[0];
                }
            }
        }
        // Fallback to default
        return $first_term;
    }
    add_filter( 'woocommerce_breadcrumb_main_term', 'prioritize_current_url_cat', 10, 2 );
    

    希望这可以帮助任何搜索时间并通过源代码行工作的人..!

    额外:修复存档页面上的永久链接

    为了解决产品存档页面上的相同问题,您可以连接到 post_type_link 过滤器并抢先用正确的 slug 替换永久链接的 %product_cat% 部分,如下所示:

    function my_plugin_product_url_use_current_cat( $post_link, $post, $leavename, $sample ) {
        if ( is_product_category() || is_product() ) {
            $product_cat_slug = get_query_var( 'product_cat', '' );
            $post_link = str_replace('%product_cat%', $product_cat_slug, $post_link);
        }
        return $post_link;
    }
    add_filter( 'post_type_link', 'my_plugin_product_url_use_current_cat', 10, 4 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      相关资源
      最近更新 更多