WooCommerce - 在面包屑中更改特定类别 link

WooCommerce - Change specific category link in Breadcrumbs

我在我的产品页面中显示面包屑。我正在尝试用自定义 link '/test' 手动替换“Horlogerie”的类别 link。

这是我的代码 :

add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 10, 2 );
function custom_breadcrumb( $crumbs, $object_class ){
  // Loop through all $crumb
  var_dump($crumbs);
    foreach( $crumbs as $key => $crumb ){
        $taxonomy = 'product_cat'; // The product category taxonomy

        // Check if it is a product category term
        $term_array = term_exists( $crumb[0], $taxonomy );

        // if it is a product category term
        if ( $term_array !== 0 && $term_array !== null ) {

            // Get the WP_Term instance object
            $term = get_term( $term_array['term_id'], $taxonomy );

            // HERE set your new link with a custom one
            $crumbs[$key][1] = 'test';
        }
    }

    return $crumbs;
}

但是 link 没有改变。我尝试使用“二十一二十一”主题,但问题仍然存在。

感谢您的帮助。

您刚刚添加了一个条件。试试下面的代码。

function custom_breadcrumb( $crumbs, $object_class ){
    // Loop through all $crumb
    var_dump($crumbs);
    foreach( $crumbs as $key => $crumb ){
        $taxonomy = 'product_cat'; // The product category taxonomy

        // Check if it is a product category term
        $term_array = term_exists( $crumb[0], $taxonomy );

        // if it is a product category term
        if ( $term_array !== 0 && $term_array !== null ) {

            // Get the WP_Term instance object
            $term = get_term( $term_array['term_id'], $taxonomy );

            if( $term->name == 'your specific term name' ){
                // HERE set your new link with a custom one
                $crumbs[$key][1] = 'test';
            }
        }
    }

    return $crumbs;
}
add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 10, 2 );