I'm trying to show the only the last child terms of a taxonomy in a post.

For example, the post "Johnny Pastafrolla" has the following terms of the taxonomy "camp" selected:

  • Summer Camp
    • Summer Camp 2018
    • Summer Camp 2019
  • Space Camp
  • Winter Camp
    • Winter Camp 2017

In this case, the displayed terms are gonna be: Summer Camp 2018, Summer Camp 2019, Space Camp, Winter Camp 2017

I found a code online which is doing this, but for Categories.

add_filter( 'the_category_list', 'ci_theme_the_category_list_remove_parent_categories', 10 );
function ci_theme_the_category_list_remove_parent_categories( $categories ) {
    $categories_tmp = $categories;
    foreach ( $categories_tmp as $child_cat ) {
        foreach ( $categories_tmp as $key => $parent_cat ) {
            if ( isset( $categories[ $key ] ) ) {
                if ( cat_is_ancestor_of( $parent_cat, $child_cat ) ) {
                    unset( $categories[ $key ] );

    return $categories;

I'm trying to "adapt it" for this specific taxonomy, but I'm kind of lost. Any hint?

Thank you


  • How are you displaying these terms? What function are you trying to modify? I believe what you want is not the last terms but last child terms, would that be correct? Asking for the last term implies a publish date, or that you want the last item in a list which would be Winter Camp 2017 or Winter Camp
    – Tom J Nowell
    Oct 31, 2020 at 0:51
  • yes exactly, the last child terms if there are any, otherwise the parent term (my example shows what I mean, the description doesn't quite :) )
    – Dande
    Oct 31, 2020 at 1:12
  • @TomJNowell I guess I found a way. By the way, I checked your site; the post about anxiety is really interesting! :) have a nice weekend!
    – Dande
    Oct 31, 2020 at 13:29

1 Answer 1


I wanted to display the terms with the get_the_term_list function so, since categories & taxonomy have a "similar logic", I replaced the "the_category_list"

add_filter( 'the_category_list', 'ci_theme_the_category_list_remove_parent_categories', 10 );

with "get_the_terms"

add_filter( 'get_the_terms', 'only_last_taxonomy_terms', 10 );

and it does what I need.

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.