Support » Plugin: Breadcrumb NavXT » Insert item between breadcrumb on specific pages

  • Resolved kingcrows

    (@kingcrows)


    Hi there,
    For a website i’m building we are using Breadcrumb NAVXT and it works great! Only at some pages we want to add custom links to the breadcrumb trail.

    For example we have this structure:
    Home -> My Category -> My Products -> My Details

    In the breadcrumb trail the “My Category” is not present when we are on the “My Products” pages. This has to do with another issue where we use some permalinks etc.

    What we would like to do is to check for specific page ids (of the “My Products” pages) and then if this page id has a match, we would like to insert the “My Category” link to the breadcrumb at that specific position in the breadcrumb.

    We can not get it working, do you have some example code or other solution how we can get this to work?

    Thanks for you time and effort!!

    • This topic was modified 3 years, 6 months ago by kingcrows.
    • This topic was modified 3 years, 6 months ago by kingcrows.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author John Havlik

    (@mtekk)

    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).

    Thread Starter kingcrows

    (@kingcrows)

    Thanks John, we’re gonna check it out!! 🙂

    Thread Starter kingcrows

    (@kingcrows)

    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 3 years, 6 months ago by kingcrows.
    Plugin Author John Havlik

    (@mtekk)

    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.

    Thread Starter kingcrows

    (@kingcrows)

    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!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Insert item between breadcrumb on specific pages’ is closed to new replies.