Home > Software engineering >  WooCommerce - Change specific category link in Breadcrumbs
WooCommerce - Change specific category link in Breadcrumbs

Time:02-15

I display the breadcrumbs inside my product pages. I am trying to manually replace the category link of "Horlogerie" with a custom link '/test'.

Here is my code using this answer :

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;
}

But the link doesn't get changed. I tried using the "Twenty Twenty-one" theme, but the problem persists.

Thank you for your help.

CodePudding user response:

You have simply just added one condition. try the below code.

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 );
  • Related