skip to Main Content

Please pardon me if there’s a better way to do this as I am not very familiar with this code. I would like to display only the link to the homepage and the current product on the breadcrumb.

Desire result:

enter image description here

Currently:

enter image description here

I found the code for the breadcrumb, is there a way to only display the first and last crumb regardless of the hierarchy?

    foreach ( $breadcrumb as $key => $crumb ) {

    echo $before;

    if ( ! empty( $crumb[1] ) && sizeof( $breadcrumb ) !== $key ) {
        echo '<a href="' . esc_url( $crumb[1] ) . '">' . esc_html( $crumb[0] ) . '</a>';
    } else if(!is_product() && !flatsome_option('wc_category_page_title')) {
        echo esc_html( $crumb[0] );
    }

    echo $after;

    // Single product or Active title
    if(is_product() || flatsome_option('wc_category_page_title')){
            $key = $key+1;
            if ( sizeof( $breadcrumb ) > $key) {
                echo ' <span class="divider">'.$delimiter.'</span> ';
            }
    } else{
        
    // Category pages
    if ( sizeof( $breadcrumb ) !== $key + 1 ) {
            echo ' <span class="divider">'.$delimiter.'</span> ';
        }
    }

}

The reason why I am doing this is that some of the products have multiple categories and by default, it will only show the breadcrumb for the primary category. I would rather make a truncated version as suggested by the owner.

I was also wondering if I can simply dynamically retrieve the product title and link + static homepage link, make it into a shortcode so that I can paste it in the product page.

2

Answers


  1. If you want to remove categories and subcategories from product breadcrumbs on the product page you can use the woocommerce_get_breadcrumb hook.

    // change the breadcrumb on the product page
    add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
    function custom_breadcrumb( $crumbs, $breadcrumb ) {
    
        // only on the single product page
        if ( ! is_product() ) {
            return $crumbs;
        }
        
        // gets the first element of the array "$crumbs"
        $new_crumbs[] = reset( $crumbs );
        // gets the last element of the array "$crumbs"
        $new_crumbs[] = end( $crumbs );
    
        return $new_crumbs;
    
    }
    

    The code has been tested and works. Add it to your active theme’s functions.php.

    A good alternative is to set the primary product category for each product. You can do this by installing the Yoast SEO plugin.

    You can use the _yoast_wpseo_primary_product_cat product meta to set the id of the primary product category.

    After setting the primary category id by editing the product in the
    backend or importing a .csv file you will only need to change the
    permalink
    and breadcrumbs based on the primary product category.

    To update the product permalink:

    // update the product permalink based on the primary product category
    add_filter( 'wc_product_post_type_link_product_cat', 'change_product_permalink_by_cat', 10, 3 );
    function change_product_permalink_by_cat( $term, $terms, $post ) {
    
        // get the primary term as saved by Yoast
        $primary_cat_id = get_post_meta( $post->ID, '_yoast_wpseo_primary_product_cat', true );
    
        // if there is a primary and it's not currently chosen as primary
        if ( $primary_cat_id && $term->term_id != $primary_cat_id ) {
            // find the primary term in the term list
            foreach ( $terms as $term_key => $term_object ) {
                if ( $term_object->term_id == $primary_cat_id ) {
                    // return this as the primary term
                    $term = $terms[ $term_key ];
                    break;
                }
            }
        }
    
        return $term;
    }
    

    To update the product breadcrumbs on the product page:

    // change the breadcrumb on the product page
    add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
    function custom_breadcrumb( $crumbs, $breadcrumb ) {
    
        // only on the single product page
        if ( ! is_product() ) {
            return $crumbs;
        }
    
        global $product;
        $new_crumbs = array();
    
        if ( $product->get_meta( '_yoast_wpseo_primary_product_cat', true ) ) {
            // gets the first element of the array "$crumbs"
            $new_crumbs[] = reset( $crumbs );
            // gets the id of the primary product category
            $primary_cat_id = $product->get_meta( '_yoast_wpseo_primary_product_cat', true );
            // create an array with all parent categories (based on the id of the primary product category)
            $parent_categories = get_ancestors( $primary_cat_id, 'product_cat' );
            $parent_categories = array_reverse($parent_categories);
            $parent_categories[] = $primary_cat_id;
            // for each product category it gets the name and the permalink
            foreach ( $parent_categories as $cat_id ) {
                $term = get_term_by( 'id', $cat_id, 'product_cat' );
                $new_crumbs[] = array(
                    0  =>  $term->name,
                    1  =>  esc_url( get_term_link( $term, 'product_cat' ) )
                );
            }
            // gets the last element of the array "$crumbs"
            $new_crumbs[] = end( $crumbs );
        } else {
            // gets the first element of the array "$crumbs"
            $new_crumbs[] = reset( $crumbs );
            // gets the last element of the array "$crumbs"
            $new_crumbs[] = end( $crumbs );
        }
    
        return $new_crumbs;
    
    }
    

    The code has been tested and works. Add it to your active theme’s functions.php.

    Login or Signup to reply.
  2. Hi – the first example in the answer above, also removes the shop from woocommerce breadcrumb. Here is a working example, that only removes the category:

    // remove only the category from woocommerce breadcrumbs
    add_filter( 'woocommerce_get_breadcrumb', 'custom_breadcrumb', 20, 2 );
    function custom_breadcrumb( $crumbs, $breadcrumb ) {
    //print the array and look for the key with the category
    //echo '<pre>';
    //print_r($crumbs);
    //echo '</pre>';
    //unset($crumbs[2]); in my case it is key 2
    
    // only on the single product page
    if ( ! is_product() ) {
        return $crumbs;
    } else {
    
      unset($crumbs[2]); // this isn't enough, it would leave a trailing delimiter
      $newBreadC = array_values($crumbs); //therefore create new array
    
      return $newBreadC; //return the new array
    }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search