Last active
December 6, 2017 04:12
-
-
Save goliver79/8824229 to your computer and use it in GitHub Desktop.
[WOOCOMMERCE] Add Product Categories to the "Product" Breadcrumb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Add product categories to the "Product" breadcrumb in WooCommerce. | |
// Get breadcrumbs on product pages that read: Home > Shop > Product category > Product Name | |
/* | |
* CAUTION: if a product is in more than one category, only show the first found category. | |
*/ | |
add_filter( 'woo_breadcrumbs_trail', 'woo_custom_breadcrumbs_trail_add_product_categories', 20 ); | |
function woo_custom_breadcrumbs_trail_add_product_categories ( $trail ) { | |
if ( ( get_post_type() == 'product' ) && is_singular() ) { | |
global $post; | |
$taxonomy = 'product_cat'; | |
$terms = get_the_terms( $post->ID, $taxonomy ); | |
$links = array(); | |
if ( $terms && ! is_wp_error( $terms ) ) { | |
$count = 0; | |
foreach ( $terms as $c ) { | |
$count++; | |
if ( $count > 1 ) { continue; } | |
$parents = woo_get_term_parents( $c->term_id, $taxonomy, true, ', ', $c->name, array() ); | |
if ( $parents != '' && ! is_wp_error( $parents ) ) { | |
$parents_arr = explode( ', ', $parents ); | |
foreach ( $parents_arr as $p ) { | |
if ( $p != '' ) { $links[] = $p; } | |
} | |
} | |
} | |
// Add the trail back on to the end. | |
$trail_end = get_the_title($post->ID); | |
// Add the new links, and the original trail's end, back into the trail. | |
array_splice( $trail, 2, count( $trail ) - 1, $links ); | |
$trail['trail_end'] = $trail_end; | |
} | |
} | |
return $trail; | |
} // End woo_custom_breadcrumbs_trail_add_product_categories() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment