You can manually add breadcrumbs to the trail by hooking into the bcn_after_fill action. An example of adding a breadcrumb to the beginning of the trail is covered in this article: https://mtekk.us/archives/guides/add-a-static-breadcrumb-to-the-breadcrumb-trail/
However, for what you want to do, you can’t use $breadcrumb_trail->add. Instead, you will want to use the PHP array_splice function to inject a new bcn_breadcrumb instance just before the last member of the $breadcrumb_trail->breadcrumbs array. You can check for the ID using $breadcrumb_trail->breadcrumbs[count($breadcrumb_trail->breadcrumbs)-2]->get_id(), though ensure you check the types as well to ensure it is of the correct type (e.g. post vs taxonomy, can retrieve the types array for a breadcrumb using the get_types() member function).
Thanks John, we’re gonna check it out!! 🙂
Could you maybe give some hints on how to add the new breadcrumb into the 2nd position of the trail?
We got the check for Page ID working, but can only the the new breadcrumb to be added at the beginning of the trail…
This is what we have so far:
add_action('bcn_after_fill', 'my_static_breadcrumb_adder');
function my_static_breadcrumb_adder($breadcrumb_trail) {
if($breadcrumb_trail->breadcrumbs[count($breadcrumb_trail->breadcrumbs)-2]->get_id() === 5450){
$breadcrumb_trail->add(new bcn_breadcrumb('Rental', NULL, array('rental'), '/rental/'));
}
}
We also tried:
add_action('bcn_after_fill', 'my_static_breadcrumb_adder');
function my_static_breadcrumb_adder($breadcrumb_trail) {
if($breadcrumb_trail->breadcrumbs[count($breadcrumb_trail->breadcrumbs)-2]->get_id() === 5450){
$original = $breadcrumb_trail->breadcrumbs;
$breadcrumb_trail->add(new bcn_breadcrumb('Verhuur', NULL, array('verhuur'), '/verhuur/'));
array_splice($original, 2, 0, $breadcrumb_trail );
}
}
But that still adds it only at the beginning of the breadcrumb trail!
Thanks!!
-
This reply was modified 7 years, 4 months ago by
kingcrows.
Don’t use breadcrumb_trail->add() as that function automatically adds the breadcrumb to then end of the breadcrumb trail (well it will show up before all of the other breadcrumbs). Instead, use something like:
$new_breadcrumb = new bcn_breadcrumb('Verhuur', NULL, array('verhuur'), '/verhuur/');
array_splice($breadcrumb_trail->breadcrumbs, -1, 0, $new_breadcrumb);
Note that I have not tried executing the above, but I believe it should work (or close to working). Also, note that in the above use of array_splice the offset of -1 tells array_splice to start at the end of the array and count backwards.
Thanks John!
We got it working with a small adjustment 🙂 We had to add: array() to the last new object in the array_splice function!
add_filter('bcn_after_fill', 'my_static_breadcrumb_adder');
function my_static_breadcrumb_adder($breadcrumb_trail) {
if($breadcrumb_trail->breadcrumbs[count($breadcrumb_trail->breadcrumbs)-2]->get_id() === 5450){
$new_breadcrumb = new bcn_breadcrumb('Rental', NULL, array('rental'), '/rental/');
array_splice($breadcrumb_trail->breadcrumbs, -1, 0, array($new_breadcrumb));
}
}
Thanks again John, awesome!