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

    /*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.

  • There's an important distinction that needs to be known to answer this question, depending on how your understand terms to work. Did you check Cinematography? Or did you check all 3 terms Cinematography Production and Film making? If it's the first one, then a really simple solution can be written as the answer, but if it's the second one... the answer will have to be more complex due to the misunderstanding. Finally, are all terms guaranteed to be in a parent/child/grandchild/etc relationship? Is this an attempt to build breadcrumbs?
    – Tom J Nowell
    Feb 5, 2020 at 17:59
  • @TomJNowell apologies for the omission. I have ticked all 3, Filmmaking, Production, Cinematography. And yes all cases will be parent/child/grandchild. I will never for example be clicking 2 parent, or 2 child terms for one post. It is a breadcrumb, it is to let a user know how deep into a particular taxonomy the post they are viewing is.
    – James
    Feb 5, 2020 at 18:10
  • 1
    Ah, that complicates things. For future reference, you only need to assign the grandchild term, ticking the parent terms is unnecessary. It's a bit like saying something is a sweater, then explicitly stating that it is also an item of clothing ( where clothing is the parent term ), it's just generally understood that sweaters are clothing, terms are inclusive and contain all the things assigned to their child terms too unless otherwise stated
    – Tom J Nowell
    Feb 5, 2020 at 18:46
  • Thanks for the advice, I did however try this and when displaying the breadcrumb all that is shown now is the grandchild term. Is there though now an easy way of coding the function to show the parent terms? I am only using test data so far.
    – James
    Feb 5, 2020 at 21:06
  • 1
    You have to manually account for them, as I said, the solution for either circumstance is different
    – Tom J Nowell
    Feb 5, 2020 at 21:29

1 Answer 1


If you want to show a term and all its parents/ancestors, you can do the following:

function display_term_parents( \WP_Term $starting_term ) {
    $terms = [];
    $term = $starting_term;
    while ( !is_wp_error( $term ) ) {
        $terms[] = $term->name;
        $term = get_term( $term->parent, 'kernal_category' );
    $terms = array_reverse( $terms );
    echo implode(' > ', $terms );

Where $starting_term is the term you want to show the ancestors/parents for.

This can be done for each term ( or just the first term ) that a post has, assuming that you've only checked the term you want, but not all of its parents too

  • Thanks for your patience but I think I'm missing something obvious. For the starting term I'm trying to feed it using get_the_terms($post->ID, 'kernal_category', true) however nothing is displayed. When I var_dump( $term) I get NULL. What is the correct format I need to have the $term value in?
    – James
    Feb 5, 2020 at 22:38
  • This isn't copy pasta code, you need a term to start with, since you already have code that lists all the terms a post has, you already have all the pieces you need. The starting term is the term you're wanting to show the rest of the breadcrumb for, aka the one you're trying to show the parent terms for, aka Cinematogrpahy in your original example. I also spotted a mistake I made, but again you need to get $starting_term from somewhere, it can't be pulled from thin air. I've also wrapped it in a function to make it a bit easier
    – Tom J Nowell
    Feb 5, 2020 at 23:44
  • Thanks for the clean up. I did realise that 'Cinematography' should be the term, I just can't figure out how to give that to the function you've created. It seems like the $starting_term parameter needs to be an object - but if I send the value from $kernal_category which is retrieved using get_the_terms nothing is displayed. Having debugged it seems to not recognise a term_id != 0 and thus the while loop never triggers. I have also tried using different functions to retrieve the term in a different format with no luck.
    – James
    Feb 6, 2020 at 0:42
  • try it now, I changed the exit condition
    – Tom J Nowell
    Feb 6, 2020 at 17:39
  • No luck, I'm now getting this error - Allowed memory size of 805306368 bytes exhausted (tried to allocate 536870920 bytes) . I'm not just copying and pasting, I've been trying to figure out where this is going wrong but I'm fairly inexperienced and drawing a blank.
    – James
    Feb 6, 2020 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.