Modify breadcrumbs list

Smart Navigation Breadcrumbs generates list of breadcrumbs based on the current page. But, you can modify this list, remove or add new items to it, or completely replace it with your own breadcrumbs.

List of crumbs

To do that, you need to hook into the filter: snb_breadcrumbs_final_list. This filter returns the list of breadcrumbs as array, where each element of array is a single crumb. Each crumb is array with several values:

  • label: title or label of the breadcrumb what users see in the breadcrumb.
  • type: type of the breadcrumb. This can be anything you want to better classify each crumbs: home, 404, page, date, archive…
  • class: string with CSS classes to add to the crumb.
  • rel: REL attribute for the crumb link. Default is empty.
  • url: URL for the crumb element.
  • current: URL for the crumb element. This is used for last crumb (current page), and if the crumbs has current, it will not have url value.

Some crumbs can have more elements. Post type elements include parameter cpt with name of the post type.

If the crumb includes URL parameter it will be displayed as link, if it has no URL parameters, it will not be linked, and that is usually what last element in the breadcrumbs look like. If you want to add more crumbs after the last in the list, you also need to change that last element to change current into url. Current and URL are both page links, but using one or the other as parameter name tells the plugin if the crumb should be link or not.

First element in the list is always Home page.

Example: Change Home crumb label

Here is how to only change label for the first crumb in the list.

add_filter('snb_breadcrumbs_final_list', 'crumb_change_first_label', 10, 2);
function crumb_change_first_label($list, $settings) {
  $list[0]['label'] = 'My Website Home';

  return $list;
}

Example: Add new crumbs at the end

Here is how to add one more crumb at the end. First, change the last generated crumb current to url. If you add more than one element, last one has to have current for page link, all others should have url for page link.

add_filter('snb_breadcrumbs_final_list', 'crumb_change_first_label', 10, 2);
function crumb_change_first_label($list, $settings) {
  $crumbs = count($list);

  if (isset($list[$crumbs - 1]['current']) {
    $list[$crumbs - 1]['url'] = $list[$crumbs - 1]['current'];
    unset($list[$crumbs - 1]['current'];
  }

  $list[] = array(
    'label' => 'My New Item',
    'type' => 'custom',
    'class' => 'my_item_class',
    'current' => 'http://www.example.com/my-custom-item-page/'
  );

  return $list;
}
Rate this article

You must be logged in to rate this.

1
0
796

Leave a Reply

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