Skip to content

Instantly share code, notes, and snippets.

@bryceadams
Created August 27, 2014 05:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bryceadams/85f75c5eac716412910b to your computer and use it in GitHub Desktop.
Save bryceadams/85f75c5eac716412910b to your computer and use it in GitHub Desktop.
<?php
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 ) ) {
foreach ( $terms as $c ) {
$parents = woo_get_term_parents( $c->term_id, $taxonomy, true, ', ', false, array( $c->term_id ) );
if ( $parents != '' ) {
$parents_arr = explode( ', ', $parents );
foreach ( $parents_arr as $p ) {
if ( $p != '' ) { $links[] = $p; }
}
}
}
// Add the trail back on to the end.
$links[] = $trail['trail_end'];
// Add the new links, and the original trail's end, back into the trail.
array_splice( $trail, 1, count( $trail ) - 1, $links );
}
}
return $trail;
} // End woo_custom_breadcrumbs_trail_add_product_categories()
add_filter( 'woo_breadcrumbs_trail', 'woo_custom_breadcrumbs_trail_add_product_categories', 10 );
function woo_get_term_parents( $id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
$chain = '';
$parent = &get_term( $id, $taxonomy );
if ( is_wp_error( $parent ) )
return $parent;
if ( $nicename )
$name = $parent->slug;
else
$name = $parent->name;
if ( $parent->parent && ( $parent->parent != $parent->term_id ) && ! in_array( $parent->parent, $visited ) && ! in_array( $parent->term_id, $visited ) ) {
$visited[] = $parent->parent;
$visited[] = $parent->term_id;
$chain .= woo_get_term_parents( $parent->parent, $taxonomy, $link, $separator, $nicename, $visited );
}
if ( $link ) {
$chain .= '<a href="' . get_term_link( $parent, $taxonomy ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
} else {
$chain .= $name.$separator;
}
return $chain;
} // End woo_get_term_parents()
@gabrielwinnberg
Copy link

This does not give me a trail of categories in breadcrumbs. What could be possible cause of error?

@yratof
Copy link

yratof commented Sep 27, 2016

The error is that the filter doesn't exist: woo_breadcrumbs_trail

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment