Non-Hierarchical Taxonomies and JSON-LD Compatibility

While Breadcrumb NavXT for some time has provided methods for generating JSON-LD formatted BreadcrumbLists, the use of non-hierarchical taxonomies within a breadcrumb trail may result in non-compliant breadcrumb trails. For the case of a post only being a member of a single term within a non-hierarchical taxonomy, since Breadcrumb NavXT 6.4, compliant JSON-LD is generated. However, when a post is a member of multiple non-hierarchical terms within the same taxonomy, we run into problems.

When using the normal display functions, all of the terms in the non-hierarchical taxonomy that the post is a member of will be displayed, separated by commas. This behavior has been more-or-less the same since support for tags was added in 2.1.0. In essence, when there are multiple terms, there is a second dimension to the breadcrumb trail for that specific breadcrumb.

Unfortunately, it does not appear that BreadcrumbList has the facility to support multi-dimensional breadcrumbs. Hence, a single term needs to be selected when there are multiple for a post. Luckily, the bcn_post_terms filter can be used to do this.

The Code

Introduced in Breadcrumb NavXT 5.4, bcn_post_terms allows us to filter out all of the terms returned by post_terms(). In this case, only the first term is wanted.

add_filter('bcn_post_terms','my_bcn_post_term_selector', 10, 3);
function my_bcn_post_term_selector($terms, $taxonomy, $id)
	//Check to ensure the terms list is an array and there is more than one item in it
	if(is_array($terms) and count($terms) > 1)
		//Return the fist item in a new array
		return array(0 => reset($terms));
	return $terms;

To get started quickly, just copy and paste the above code into a site specific plugin and start playing.

Note: This code relies on behavior introduced in Breadcrumb NavXT 6.4 and will not work in prior versions.

-John Havlik

3 thoughts on “Non-Hierarchical Taxonomies and JSON-LD Compatibility

  1. Thanks for that example, but for some reason, I cannot get the bcn_post_terms filter to fire the callback function. No problem with other filters like bcn_breadcrumb_linked.

    I pasted the exact code above in to test it. I tried higher and lower priority parameters. I tried it in functions.php and in a functionality plugin. No matter what I’ve tried, I can’t get the callback function to fire.

    Again, the code is identical to what’s above. Any guesses as to what might be interfering?

    • Hi Tom,

      Based on your second comment, it sounds like you are trying to target hierarchical terms. Currently, there are two different code paths for hierarchical and non-hierarchical terms. bcn_post_terms is only available in the non-hierarchical term code path. For hierarchical terms, your best shot at influencing what happens there is with bcn_pick_post_term. Otherwise, the bcn_after_fill action can be used to modify the the contents of the breadcrumbs array in the instance of the bcn_breadcrumb_trail object that is passed in by reference as on of the parameters.

      -John Havlik

  2. Incredible plugin, by they way. This is the second site I’ve used it on. Previous site I didn’t need to get into the hooks at all because it does so much out of the box.

    In the present case, it’s because the site in question has a rather deep hierarchy with very long category names and post names, so I basically want to filter the taxonomy terms by level – if the term has no parent, add it to the breadcrumbs and if it’s a child term, filter it out. Not ideal, but neither is showing everything in this case.

Comments are closed.