How to Add Custom Taxonomy Breadcrumbs in WordPress

Home » Snippets » How to Add Custom Taxonomy Breadcrumbs in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

When it comes to organizing and categorizing your content in WordPress, taxonomies are an essential tool. By default, WordPress offers built-in taxonomies like categories and tags. However, if you want to take your content organization to the next level, you’ll need to create custom taxonomies. In this article, we’ll explore how to implement a custom taxonomy breadcrumb in WordPress, allowing your users to easily navigate through your website’s hierarchical structure and find the content they’re looking for.

					function wpturbo_custom_taxonomy_breadcrumb() {
    $term = get_queried_object();
    $taxonomy = $term->taxonomy;
    $term_id = $term->term_id;
    
    $ancestors = array_reverse(get_ancestors($term_id, $taxonomy));
    
    foreach ($ancestors as $ancestor) {
        $ancestor_term = get_term_by('id', $ancestor, $taxonomy);
        $ancestor_link = get_term_link($ancestor_term, $taxonomy);
        
        echo '<a href="' . esc_url($ancestor_link) . '">' . esc_html($ancestor_term->name) . '</a> / ';
    }
    
    echo esc_html($term->name);
}
add_action('wpturbo_custom_taxonomy_breadcrumb', 'wpturbo_custom_taxonomy_breadcrumb');
				

The first part of the code snippet defines a new function called wpturbo_custom_taxonomy_breadcrumb(). This function will be responsible for creating and displaying the breadcrumb navigation for a custom taxonomy in WordPress.

To start, we retrieve the current queried object using the get_queried_object() function. This gives us access to information about the current term in the custom taxonomy.

Next, we retrieve the taxonomy name and the term ID from the queried object:

$term = get_queried_object();
$taxonomy = $term->taxonomy;
$term_id = $term->term_id;

We then use the get_ancestors() function to retrieve an array of the ancestor term IDs for the current term. We use array_reverse() to reverse the order of the array, so that the ancestors are displayed from top to bottom:

$ancestors = array_reverse(get_ancestors($term_id, $taxonomy));

Using a foreach loop, we iterate through each ancestor term ID and retrieve its corresponding term object and term link using the get_term_by() and get_term_link() functions. We then echo out an anchor tag that includes the term name and the term link:

foreach ($ancestors as $ancestor) {
    $ancestor_term = get_term_by('id', $ancestor, $taxonomy);
    $ancestor_link = get_term_link($ancestor_term, $taxonomy);

    echo '<a href="' . esc_url($ancestor_link) . '">' . esc_html($ancestor_term->name) . '</a> / ';
}

Finally, we echo out the name of the current term, using esc_html() to ensure that the term name is properly escaped:

echo esc_html($term->name);

To display the breadcrumb navigation, we hook the wpturbo_custom_taxonomy_breadcrumb() function into the wpturbo_custom_taxonomy_breadcrumb action:

add_action('wpturbo_custom_taxonomy_breadcrumb', 'wpturbo_custom_taxonomy_breadcrumb');

Once hooked, you can call the breadcrumb navigation in your theme or plugin by using the do_action() function and specifying the wpturbo_custom_taxonomy_breadcrumb action:

do_action('wpturbo_custom_taxonomy_breadcrumb');

This will generate the breadcrumb navigation for the current term in your custom taxonomy, including all its ancestor terms.

Register an account to save your snippets or go Pro to get more features.