I am trying to get the term children and then check if they exist to display them. But I get no IDs in return of get_term_children command. This is the code:

  $children = get_term_children($term->term_id, locations);
  echo $children;

$term is already defined and outputs well, "locations" taxonomy exists. The code returns me just an "Array" word, no list of child terms shows up.

I've got a similar code in my post archive pages and it works well returning me child categories in n/n/n/n/n format:

  $children = get_category_children($this_category->cat_ID);
  echo $children;

What's wrong with the first code? It should work as well as the second.


1 Answer 1


At first, try passing the Taxonomy as a string:

$children = get_term_children($term->term_id, 'locations');

The function get_category_children produces a string as return, so you can just echo it. the function get_term_children, however, returns an array. To see the contents of the array, try

print_r( $children );

instead of echo.

The last thing to keep in mind is that a taxonomy does not need to be hierarchical - for example the built in tags. Be sure your "locations" is organized hierarchical, otherwise get_term_children won't work for you in this case.

  • Thank you for the fast reply, fischi. My taxonomy is hierarchical, of course. I think I've got the thing, get_category_children and get_term_children return different types of data, that's why echo doesn't work. I tried to output the array via print_r, and it worked well. My problem is I want to check if term has children. For category I have this code: if (get_category_children($this_category->cat_ID) != "") And what kind of code could you suggest for a taxonomy term?
    – Jerry
    Apr 30, 2012 at 14:23
  • Kinda solved it as well: if (get_term_children($term->term_id, 'locations') != null). Works fine. Thank you again.
    – Jerry
    Apr 30, 2012 at 14:34

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.