Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
See also: wordpress.stackexchange.com/a/15289/73 to list sub terms. –  toscho May 1 '12 at 10:50
add comment

1 Answer

up vote 2 down vote accepted

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.

share|improve this answer
    
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 '12 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 '12 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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