The get_term_children() is a WordPress builtin function and it will merge all term children into a single array. This recursive function will merge all of the children of $term into the same array. Only useful for taxonomies which are hierarchical.
Will return an empty array if $term does not exist in $taxonomy.
Return Values:
array|WP_Error) Array of Term IDs. WP_Error returned if $taxonomy does not exist.
Examples 1: get_term_children() function:
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $term_id = 10; $taxonomy_name = 'products'; $term_children = get_term_children( $term_id, $taxonomy_name ); echo '<ul>'; foreach ( $term_children as $child ) { $term = get_term_by( 'id', $child, $taxonomy_name ); echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>'; } echo '</ul>'; ?> |
Output:
1 2 3 4 |
<ul> <li><a href="link_to_term_page">Term 1</a></li> <li><a href="link_to_term_page">Term 2</a></li> </ul> |
Source File:
get_term_children() is located in wp-includes/taxonomy.php.
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.