How to Add a Custom Breadcrumb Template Tag

Since version 4.4.0, it has been possible to add additional custom template tags to supplement the defaults included in Breadcrumb NavXT. Adding a custom template tag is relatively easy. All one has to do is write a function that hooks into the bcn_template_tags filter and adds the new template tag.

bcn_template_tags has three parameters, two for helping identify the resource the breadcrumb represents, and one for the actual replacement set. Today, we’ll focus on the first parameter, the $replacements array. This associative array has the template tag as the key and the value is the actual value that should replace the tag in breadcrumbs. To add a new template tag, simply add a new key-value pair to the array and return the modified $replacements array.

For example, to add a breadcrumb template tag that inserts the current theme’s directory, one could use the following:

function my_bcn_template_tag($replacements, $type, $id)
{
    //Add the %template_directory% template tag
    $replacements['%template_directory%'] = get_bloginfo('template_directory');
    //Return our new set of templates and replacements
    return $replacements;
}
add_filter('bcn_template_tags', 'my_bcn_template_tag', 3, 10);

In the code above, %template_directory% is the breadcrumb template tag being added; its value is retrieved using the get_bloginfo() function. Simple, right?

-John Havlik

[end of transmission, stay tuned]

4 thoughts on “How to Add a Custom Breadcrumb Template Tag

  1. Would I use this filter to replace the link labels used in the breadcrumbs that are taken by html page titles by default and replace them with my own labels? If so, could you show an example?

    If I have:
    Label from HTML title > Label from HTML title > Label from HTML title

    and want to customize breadcrumbs to be:
    Custom Label > Custom Label > Custom Label >

  2. Like this?:

    function my_bcn_template_tag($replacements, $type, $id)
    {
    $replacements['%Custom Label%'] = get_bloginfo('Label from HTML title');
    return $replacements;
    }
    add_filter('bcn_template_tags', 'my_bcn_template_tag', 3, 10);

Comments are closed.