Conditionally Remove Home from the Breadcrumb Trail

Since Breadcrumb NavXT 3.5.0, two WordPress actions have been added into Breadcrumb NavXT. They are the bcn_before_fill and bcn_after_fill actions. As their names suggest, the first runs at the beginning of bcn_breadcrumb_trail::fill(), and the second runs at the end of the same function. Both actions pass a reference to the current bcn_breadcrumb_trail instance into the hooked function. This post quickly covers a use case for the bcn_after_fill action.

Say for some reason, you want to exclude the home breadcrumb from the breadcrumb trail on only certain pages. Before you had to use object oriented programming techniques to do this. Now you only need 8 lines of code (including spaced braces for clarity). Simply place the code below in your functions.php, and change ‘page_foo’ to what ever page name/id you do not want to see the home breadcrumb on.

add_action('bcn_after_fill', 'foo_pop');
function foo_pop($trail)
{
  if(is_page('page_foo'))
  {
    array_pop($trail->breadcrumbs);
  }
}

Note that this will remove the last breadcrumb to be placed on the trail, which means you could end up removing a different breadcrumb depending on your site’s configuration (static front page vs just a blog, vs a multisite setup). In Breadcrumb NavXT 3.8.0 many of the trail members will be named, allowing easier access for removing the correct breadcrumb.

-John Havlik

[end of transmission, stay tuned]

17 thoughts on “Conditionally Remove Home from the Breadcrumb Trail

    • Could you be a little more specific? Where are you placing the code? What version of Breadcrumb NavXT? What do you mean by “it just breaks the whole website”?

      -John Havlik

    • Roberta,

      To do that, just change the is_page('page_name') to is_front_page(). Or do things the correct way for that type of a page and check that WordPress conditional in an if statement surrounding your bcn_display or bcn_display_list function call.

      -John Havlik

  1. Thank you for this plug-in, very helpful.

    I simply do not want the current page title to show in the breadcrumb (it is a bit redundant, I have the page title right below it). I see the code above that removes it from a specific page or post. What is the code to remove the post title from all my posts and pages.

    thanks for your help!

  2. Hi John,

    Let’s say i would like to add an item in breadcrump path when i’m on a specific category. is it possible to do that with your plugin ?

    for instance if i’m in category 1 i would like to have :
    Home > cutom item > category 1

    Thanks

    Odo

  3. Can anyone help me to show home page title instead of site name
    like currently it is showing like sitename > page1 > ….

    but i am i need title of my home page which is setup as frontpage
    home > page1 >…
    it is because i am using qtranslate plugin to create multilanguage pages
    so i cannot add static Home text in the configuration of Breadcrumb NavXT

    • There currently isn’t a great way of dealing with this. Normally, I’d say change from %title% to “Home” or whatever you want to call it. However, that’s not necessarily workable. I know with other translation plugins (WPML and polyglot), there is a string translation table. There are extension plugins for Breadcrumb NavXT to deal with translation of the setting strings (breadcrumb templates in particular). I assume something similar could be written for qTranslate (though it looks like qTranslate has been abandoned, the fork qTranslate X seems to be the successor).

      -John Havlik

Leave a Reply

Your email address will not be published. Required fields are marked *