I would like to display a list of child terms according to my current taxonomy.

Taxonomy name ='course'

  • Photoshop(=child term)

    • ps-thematic#1(= child child term)
    • ps-thematic#2
    • ...
  • After Effects

    • ae-thematic#1

    • ae-thematic#2

    • ...

  • Another course

    • zz-thematic#1

So, in my taxonomy-course-photoshop.php, I'd like to display ps-thematic#1, ps-thematic#2, etc.
I found plenty of questions and answers on this subject.
I tried many them.
Without success...
So now, I just feel very confused between get_terms, get_children_term. Or $args= array('parent' => $this_term->term_id,...}

My last try was this topic, which seems perfect, but I get a message error : Catchable fatal error: Object of class WP_Error could not be converted to string in

Thank you for any help.

1 Answer 1


I think you can just use get_terms() for this. This is just some bare minimum code, I guess you also want to add permalinks, term description or other things.

However, I just tested this code (now tested with taxonomy "course") and it works.

I try my best to comment the code as we go along:

// our current taxonomy slug
// If you want to get the current taxonomy automatically try using $wp_query->get_queried_object();
    $taxonomy = 'course';

// we get the terms of the taxonomy 'course', but only top-level-terms with (parent => 0)
$top_level_terms = get_terms( array(
    'taxonomy'      => $taxonomy,
    'parent'        => '0',
    'hide_empty'    => false,
) );

// only if some terms actually exists, we move on
if ($top_level_terms) {

    echo '<ul class="top-level-terms">';

    foreach ($top_level_terms as $top_level_term) {

        // the id of the top-level-term, we need this further down
        $top_term_id = $top_level_term->term_id;
        // the name of the top-level-term
        $top_term_name = $top_level_term->name;
        // the current used taxonomy
        $top_term_tax = $top_level_term->taxonomy;

        // note that the closing </li> is set further down, so that we can add a sub list item correctly
        echo '<li class="top-level-term"><strong>'.$top_term_name.'</strong>';

        // here we get the child-child terms
        // for this we are using 'child_of' => $top_term_id
        // I also set 'parent' => $top_term_id here, with this line you will only see this level and no further childs
        $second_level_terms = get_terms( array(
            'taxonomy' => $top_term_tax, // you could also use $taxonomy as defined in the first lines
            'child_of' => $top_term_id,
            'parent' => $top_term_id, // disable this line to see more child elements (child-child-child-terms)
            'hide_empty' => false,
        ) );

        // start a second list element if we have second level terms
        if ($second_level_terms) {

            echo '<ul class="second-level-terms">';

            foreach ($second_level_terms as $second_level_term) {

                $second_term_name = $second_level_term->name;

                echo '<li class="second-level-term">'.$second_term_name.'</li>';

            }// END foreach

            echo '</ul><!-- END .second-level-terms -->';

        }// END if

        echo '</li><!-- END .top-level-term -->';

    }// END foreach

    echo '</ul><!-- END .top-level-terms -->';

}// END if

Also try using print_r($top_level_term); for example in the first foreach loop. With that, you can see all the info that comes with one single term.

I would say you should use this code in the normal taxonomy archive template and don't use the file taxonomy-course-photoshop.php. Because then you would need a separate template file for every single term, and that's not practicable.

Edit: Also, make sure you have some posts assigned to the single terms and sub-terms. Otherwise, the terms will not display at all!

Update: Yes, you can get the current taxonomy and term data automatically.

If you are working inside taxonomy-course.php than you can try this:

        //get the current object
        $current = $wp_query->get_queried_object();

        // try  var_dump($current);  to see all available data!

        // return the ID of the current term
        // i.e. ID of term "Photoshop" is "26", so we get "26" if we are viewing "Photoshop"
        $current_term_id = $current->term_id;

        // return the nicename of the current term
        // i.e. returns "Photoshop"
        // or "ps-thematic#1" if we are on a child term of "Photoshop"
        $current_name = $current->name;

        // returns the current taxonomy slug we are in
        // i.e. it will return "course"
        $current_taxonomy = $current->taxonomy;

        // returns the ID of the parent, if we have a parent
        // i.e. if we are viewing "ps-thematic#1" it will return the ID of "Photoshop", 26
        // if we are viewing "Photoshop", it will return 0, because "Photoshop" is a top level term
        $current_parent = $current->parent;

        // display name of current term, i.e. "Photoshop"
        echo '<strong>'.$current_name.'</strong>';

        $sub_terms = get_terms( array(
            'taxonomy'      => $current_taxonomy,
            'child_of'      => $current_term_id,
            'hide_empty'    => false,
        ) );

        // only start if some sub terms exist
        if ($sub_terms) {

            // try  var_dump($sub_terms);  to see all available data!

            echo '<ul class="sub-terms">';

            foreach ($sub_terms as $sub_term) {

                // try  var_dump($sub_term);  to see all available data!

                // only show the name for the example, "ps-thematic#1"
                echo '<li>'.$sub_term->name.'</li>';

            }// END foreach

            echo '</ul><!-- END .sub-terms -->';

        }// END if

So if we are viewing "Photoshop", the above function shows ONLY:
- ps-thematic#1
- ps-thematic#2

It does NOT show "After Effects" or the children, ae-thematic#1 or ae-thematic#2.

Also take a look using var_dump($current);. You will see all available information. As you will see above, for example, I left some code in the function $current_parent = $current->parent;.
For example you could check:

            // check if current parent is not = 0
            if ($current_parent != '0') {
                // code if parent is NOT 0
                // that always means that we are viewing a child
            } else {
                // code if parent is 0
                // if parent is 0 we are sure we are on a top level

But there are some more infos/data in $current that you can maybe use. I Hope this helps.

  • Thank you, it works indead ! Thank you more because your answer is so well commented, patient & pedagogic ! Stay one problem : the terms (parents and children) are well displayed. But ALL of the terms of my taxonomy. I'm trying to display only the child term of my curent parent.
    – Cha
    Commented Mar 9, 2017 at 9:16
  • I mean : $top_level_terms = current parent term (example : Photoshop) (I am in taxonomy-course.php) $second_level_terms = child parent from my parent (eg : thematic-photoshop01, thematic-photoshop01...) You wrote : $top_level_terms = get_terms(array...) Is ti possible to add something like to get the current term ? $top_level_terms = get_queried_object()->name; But again, I thank you a lot for the time that you've already spent
    – Cha
    Commented Mar 9, 2017 at 9:27
  • @cha I just updated my answer, see if that helps. I also recreated your taxonomy / term structure and was now also using taxonomy-course.php template file. Just to be sure that it works in this file.
    – LWS-Mo
    Commented Mar 10, 2017 at 9:57
  • Brilliant !!! It's exactly what I needed !! And thanks a lot (again) for the quality of your explanations.
    – Cha
    Commented Mar 10, 2017 at 10:47

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.