I want to customize the breadcrumbs directly from PHP. Some of the pages are generated dynamically and do not exist in the database, therefore i must automatically put them in the breadcrumbs with some sort of PHP script.
I don’t need to change default stuff, like homepage url, separators, etc…but i actually need to manually put some pages in the breadcrumbs. I was trying with some filterings and some hooks.
I’ve read the documentation but it just explains how to change default stuff.
How can i change the actual content of the breadcrumbs?
I tried this:
add_filter( 'woocommerce_breadcrumb', 'change_breadcrumb' );
function change_breadcrumb( $defaults ) {
// Change the breadcrumb home text from 'Home' to 'Appartment'
//do something
return $defaults;
}
But the //do something
never executes. It’s like if that filter never gets called
1 Answer
That is due to the fact, that your filter woocommerce_breadcrumb
doesn’t even exist.
This filter here works and pulls out all the elements, that are currently in the breadcrumb (as an array):
add_filter( 'woocommerce_get_breadcrumb', 'change_breadcrumb' );
function change_breadcrumb( $crumbs ) {
var_dump( $crumbs );
return $crumbs;
}
And this filter pulls out the main term
(as an object).
add_filter( 'woocommerce_breadcrumb_main_term', 'change_breadcrumb' );
function change_breadcrumb( $main_term ) {
var_dump( $main_term );
return $main_term;
}
The ‘main term’ is just the first element that is returned by this function (reference):
$terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) )
See the Action and Filter Hook Reference by woothemes for all hooks & filters.