Modify the breadcrumb element

I know I can modify the automatically generated breadcrumbs via php and I have done so using…

function x_breadcrumbs() {
if ( x_get_option( 'x_breadcrumb_display' ) ) {

However, since I do not want to place it in the mast head, I am using the breadcrumb element in cornerstone. My modifications do not show up in the element even though they are working fine if I display the masthead. Is there a way to do this?

Hi There,

Thanks for writing in! Could you please explain the changes that you require, so that we can assist you if your changes are possible.

Thanks!

I am changing the actual structure based on a custom post type taxonomy. I have a CPT of employees, and several employee types. I do not want to use the standard archive pages, so I am creating a traditional “page” for the archive. I have modified the bread crumbs to omit the post type link and add the new page if certain terms are present. This works fine if I turn on breadcrumbs in the masthead. However it is not reflected in the element.

Howdy, @Solidwebservice!

Thanks for writing in! In the latest version of the theme, the “classic” breadcrumbs and the Breadcrumbs Element both utilize the same source data and functions for crafting their markup. You can see these by going to the following file in your theme:

/x/framework/functions/global/breadcrumbs.php

The x_breadcrumbs() function is the classic function used in our standard headers, but we also have 2 new functions that we introduced a while back when we updated these, x_breadcrumbs_items() and x_breadcrumbs_data(). Effectively, these two functions operate like so:

  • x_breadcrumbs_items() – sets up the markup used for the breadcrumbs, which can be filtered using the x_breadcrumbs_items hook in a child theme (you can also filter x_breadcrumbs_items_args to make adjustments to things like the opening and closing tags, delimiter, et cetera).
  • x_breadcrumbs_data() – sets up the data used for the breadcrumbs, which can be filtered using the x_breadcrumbs_data hook in a child theme (you can also filter x_breadcrumbs_data_args to make adjustments to how certain core labels should be output).

For your situation, it sounds like you’re more interested in updating the data of the breadcrumb trail, which can be done using the following technique:

function my_custom_breadcrumbs( $crumbs, $args ) {

  foreach ( $crumbs as $i => $crumb ) {
    if ( $crumb['type'] === 'page' && $crumb['label'] === 'Test' ) {
      $crumbs[$i]['label'] = 'New Label';
    }
  }

  return $crumbs;

}

add_filter( 'x_breadcrumbs_data', 'my_custom_breadcrumbs', 10, 2 );

Here, we’re filtering the x_breadcrumbs_data hook using our custom my_custom_breadcrumbs() function. Inside the function, we’re looping through each “crumb” in the breadcrumb trail, and once we’re inside that loop, we can perform checks to see if certain conditions are matched. If a match is found, we can update the original array ($crumbs) and then return our new result at the end (please note that $crumbs must always be returned at the end of this function or else you will run into errors).

Each “crumb” of data will contain three points for you to do checks on:

  • type – the type of breadcrumb being output, which will most often be the actual post type, but can be different different depending on how it is setup in x_breadcrumbs_data()
  • url – the associated url to be used on the breadcrumb item
  • label – the text label used for the breadcrumb item

In the example above, we’re checking to see if the crumb is a page ($crumb['type'] === 'page') as well as if the label of that page is “Test” ($crumb['label'] === 'Test'). If it is, we update that particular crumb in the main array to output our new label (or url, whatever you want to update) to our new value;

You can take these basic principals and apply it however you need to create your own conditions, you will just need to make sure to output the raw $crumbs data to see what is output for you to work with, and then create your conditions from there. To see this data easily, you can use our native x_dump() function, which we use for quick debugging situations like this. You can place the following on the first line of the function before anything else to see that data:

x_dump( $crumbs );

Hopefully that all helps to point you in the right direction. Cheers!

That did it. Thank you.

You’re most welcome!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.