Correctly order the hierarchy of custom taxonomy when displaying terms

On my page I am displaying the custom taxonomy terms relevant to a post. For example a post may have a heirachy of:

Filmmaking -> Production -> Cinematogrpahy

To do this I am using the below code in my content-child page

<?php
/*Retrieve Category Name */
if(get_the_terms($post->ID, ‘kernal_category’, true)) {
// Create an empty array

$kernal_category = get_the_terms($post->ID, ‘kernal_category’, true);

$categories = [];

// Save each category to the array
foreach ($kernal_category as $category) {

$categories[] = $category->name;
}

// Now output with implode
echo implode(‘ > ‘, $categories);
}
?>

My problem is that when these terms are displayed they are not following the correct hierachy. Using the example above my post is actually displayed as follows: Cinematography -> Production -> Filmmaking.

I have already checked and the primary for this taxonomy is set correctly i.e. the Primary for Cinematography is Filmmaking

How do I display them hierarchically?

Update: It appears to affect taxonomies with more than a Primary and Secondary term. All posts with just primary and secondary are displaying correctly.

Read more here:: Correctly order the hierarchy of custom taxonomy when displaying terms

Leave a Reply

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