Adding Shop Page Title & Primary Category in WC Breadcrumb
You created the Single product layout using Beaver Themer and added the Woo Breadcrumb module. You are wanting to edit the default breadcrumb structure and will add the shop page title and primary category there. So it would be look like this Home / Shop / Primary Category Name / Product Title. In this article I am showing how you will get it.
How do you make a primary category
If you install the Yoast SEO plugin, you can generate the primary category for a post. See the attached file
Filter the WooCommerce Breadcrumb
Editing the default breadcrumbs array value with woocommerce_get_breadcrumb filter. Open the functions.php file of your active theme and add this PHP snippet. Before editing the file you will keep a backup of your theme.
Filter the crumbs array
add_filter( 'woocommerce_get_breadcrumb', 'probb_filter_wc_crumbs', 10, 2 ); function probb_filter_wc_crumbs( $crumbs, $object ) { //* Checking Single Product Page if( is_singular('product') ) { //* Do nothing if shop page set as static front page if ( get_option( 'page_on_front' ) == wc_get_page_id( 'shop' ) ) { return $crumbs; } $permalinks = get_option( 'woocommerce_permalinks' ); //* getting premalink structure $arr_shift = array_shift( $crumbs ); //* removing the first element "Home" $new_crumbs = array(); //* Premalink is default then adding the shop page title into crumbs array if ( empty( $permalinks['product_base'] ) ) { $_name = wc_get_page_id( 'shop' ) ? get_the_title( wc_get_page_id( 'shop' ) ) : ''; if ( ! $_name ) { $product_post_type = get_post_type_object( 'product' ); $_name = $product_post_type->labels->singular_name; } $new_crumbs = array( array( $_name, get_post_type_archive_link( 'product' ) ) ); } /** * Checking Yoast Plugin active * If it is there, then fetching the primary category name and * adding into crumbs array */ if ( class_exists( 'WPSEO_Breadcrumbs' ) ) { global $post; $terms = get_the_terms( $post, 'product_cat' ); if ( is_array( $terms ) && $terms !== array() ) { $primary_term = new WPSEO_Primary_Term( 'product_cat', get_the_ID() ); if ( $primary_term->get_primary_term() ) { if ( ! empty( $permalinks['product_base'] ) ) { $new_crumbs[] = array_shift( $crumbs ); } $breadcrumb_term = get_term( $primary_term->get_primary_term(), 'product_cat' ); $ancestors = get_ancestors( $breadcrumb_term->term_id, 'product_cat' ); $ancestors = array_reverse( $ancestors ); foreach ( $ancestors as $ancestor ) { $ancestor = get_term( $ancestor, 'product_cat' ); if ( ! is_wp_error( $ancestor ) && $ancestor ) { $new_crumbs[] = array( $ancestor->name, get_term_link( $ancestor ) ); } } $new_crumbs[] = array( $breadcrumb_term->name, get_term_link( $breadcrumb_term ) ); $last_element = array_pop( $crumbs); $crumbs = array_merge( array($arr_shift), $new_crumbs, array($last_element) ); return $crumbs; } } } //* Making the new crumbs array $crumbs = array_merge( array($arr_shift), $new_crumbs, $crumbs ); } //* Returning the crumbs array for process return $crumbs; }
Posted in Beaver Builder