How to create breadcrumbs from post categories

How to create breadcrumbs from post categories

As you probably know, WordPress is a very widespread CMS — and people try to use it just about everywhere. Working on some projects, you can’t help but wonder: why use it here? Wouldn’t it be simpler and more efficient to use something built on a PHP framework? But trends are what they are — WordPress is used where it’s needed, and often where it isn’t.

Today’s post is not about whether WordPress should or shouldn’t be used in certain projects. Instead, you’ll learn how to generate a term array or build breadcrumbs for a post. It doesn’t matter what post type you’re using — a regular post or a custom post type — or whether the taxonomy is built-in or custom.

Function to Generate an Array of Post Terms

Let’s get to it. Our code will consist of two parts. The first one is a function; the second is regular PHP logic.

function get_hierarchical_terms_post($post_id, $taxonomy)
{
	$terms = get_the_terms($post_id, $taxonomy);

	if($terms == false || is_wp_error($terms))
	{
		return array();
	}

	$term = array_shift($terms);
	$array = array($term);
	$parent_id = $term -> parent;

	while($parent_id)
	{
		$term = get_term_by('id', $parent_id, $term -> taxonomy);
		$parent_id = $term -> parent;
		$array[] = $term;
	}

	$array = array_reverse($array);

	return $array;
}

Let’s break this down. The function is universal — it takes two parameters: the post ID and the taxonomy name. Then it uses the get_the_terms function, which returns an array of post term objects. If the result is an error or false, the function exits and returns an empty array.

We grab the first term and begin building a linear “tree” of parent terms as an array. The variable $parent_id holds the ID of the parent term. Then we use a while loop to repeatedly call get_term_by and fetch each parent term. We then update $parent_id for the next loop iteration:

$parent_id = $term -> parent;

And add the parent term to our array:

$array[] = $term;

The loop continues until $parent_id equals zero.

Finally, we reverse the array using array_reverse so that terms go from top-level to child, and return the result.

Building Post Breadcrumbs

To build breadcrumbs, we write the following code:

$Terms = get_hierarchical_terms_post(get_the_ID(), 'category');

$category_link_ar = array();
foreach($Terms as $Term):
    $category_link_ar[] = '<a href="'.get_term_link($Term -> term_id, 'category').'">'.esc_attr($Term -> name).'</a>';
endforeach;

echo join(' &raquo; ', $category_link_ar);

Note: it’s best to use this code within the content template — this ensures the category slugs are properly formed.

We pass the current post ID and taxonomy name as arguments to get_hierarchical_terms_post.

Then we loop through the returned terms and build links. To get the term URL, we use the standard WordPress function get_term_link.

Finally, we use PHP’s join function to combine the term links into a breadcrumb string, separated by the right arrow symbol ».

That’s it!

Posts on similar topics

Are you having problems with your WordPress site? Do you need additional functionality? A custom plugin or a new page?
Then write to me via the feedback form, and I will try to help you.

Write a comment

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