Disable Individual Category Breadcrumbs in NavXT

When it comes to adding breadcrumbs to a site, we normally use Breadcrumbs NavXT. And we love it, but sometimes it just needs a little tweaking.

Recently when configuring a new blog for a client that makes heavy use of categories, we ran into a scenario where we needed to remove the category slug from the breadcrumbs, but only for uncategorized posts. NavXT has no way out of the box to make this happen, while continuing to use categories in the breadcrumb structure for other posts.

“Uncategorized” is neither attractive nor helpful…

Step 1: Determine which categories to disable in the breadcrumbs

The first thing we need to do is create a way to determine which categories should be displayed, and which should not. Hardcoding this is not an option, or shouldn’t ever be since it stops the end user from being able to use it effectively.

This is where Advanced Custom Fields comes in handy. We use ACF extensively, and include it in all of our custom builds. Using ACF we can create settings and metaboxes’ virtually anywhere we want, in this case on the category settings page.

Create a new fieldgroup, set to display on categories

Next, add a new settings field. All we need is a true/false to enable (default) or disable the category in the breadcrumbs.

With our shiny new settings field enabled, if we head over to the category edit page for the category “Uncategorized” we’ll see our setting available, allowing us to save term meta.

Step 2: Remove selected categories from the breadcrumb, using a filter.

Woohoo…we’ve saved data to the term object, which we can access to determine if a category should be disabled, but we still haven’t actually removed it from the breadcrumbs yet.

After a bit of jiggery pokery inside the plugin, I found this filter: bcn_pick_post_term which allows us to override the WP_Term object selected for any given post. If this filter returns something other than a WP_Term object, it is bypassed.

From NavXT, class.bcn_breadcrumb_trail.php

We’re almost there…

Now, all we have to do is create a function that filters the results, returning something other than a term object if the selected category is disabled. Since this filter gives us the term object, we don’t even need to check the post we’re on, just use the provided term object and get our custom term_meta. If it’s true, return false. Simple.

But let’s go ahead and walk through it.

  1. First, we make sure we’re on a singular of the post_type ‘post’. There’s no reason to waste time if we aren’t.
  2. Second, we ensure that the term passed is, in fact, an instance of WP_Term. If there’s an error upstream somewhere, we can just bail.
  3. Here’s where the fun is at. This is where we check the term meta, using the ID of the provided term. If it’s true, we return false instead of an object and short-circuit the term.
  4. And finally, for good measure, we return the original term for everything else.
/**
 * Filter WP_Term object provided by NavXT for breadcrumbs
 *
 * @param  object $wp_term - An instance of the chosen WP_Term
 * @param  int $id - ID of the post
 * @param  string $type - The current post type
 * @param  string $taxonomy - The type of the chosen taxonomy
 * @return object/bool Either the original $wp_term or false
 */
function _wpcl_bcn_pick_post_term( $wp_term, $id, $type, $taxonomy ) {
	/**
	 * Make sure we're on a single post
	 */
	if ( !is_singular( 'post' ) ) {
		return $wp_term;
	}
	/**
	 * Make sure we have a WP_Term object to work with
	 */
	if ( !is_a( $wp_term, 'WP_Term' ) ) {
		return $wp_term;
	}
	/**
	 * Check for previously created term meta
	 */
	if ( get_term_meta( $wp_term->term_id, 'disable_category_breadcrumb', true ) === '1' ) {
		return false;
	}
	/**
	 * If we made it here, just return the term
	 */
	return $wp_term;
};
add_filter( 'bcn_pick_post_term', '_wpcl_bcn_pick_post_term', 10, 4 );

Drop this in your functions.php or a site specific plugin. Now you can enable/disable the category slug on a per-category basis, allowing you to take a little bit more control over your site structure. The ugly and useless “uncategorized” is gone, and we maintain the ability to show the category for other, more meaningful categories.

Published by

I’m a full stack Web Developer living and working in Madison, WI. I specialize in WordPress development, including plugins, themes, security, performance, and maintenance.

Leave a Reply

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