get_term_children()
Merge all term children into a single array of their IDs.
This recursive function will merge all of the children of $term into the same array of term IDs. Only useful for taxonomies which are hierarchical.
Will return an empty array if $term does not exist in $taxonomy.
Uses: _get_term_hierarchy()
1 time — 0.000048 sec (very fast) | 50000 times — 0.75 sec (very fast) | PHP 7.0.5, WP 4.5.2
No Hooks.
Return
Array|WP_Error
. List of term IDs. WP_Error returned if $taxonomy does not exist.
Usage
get_term_children( $term_id, $taxonomy );
- $term_id(int) (required)
- ID of term to get children.
- $taxonomy(string) (required)
- Taxonomy name.
Examples
#1 Usage example
Let's display the names of the child sections of taxonomy element 10, as links to the corresponding pages of the archives:
$term_id = 10; // get_queried_object()->term_id; - current category ID (dynamic) $tax_name = 'products'; $term_childs = get_term_children( $term_id, $tax_name ); echo '<ul>'; foreach ( $term_child as $child ) { $term = get_term_by( 'id', $child, $tax_name ); echo '<li><a href="' . get_term_link( $term ) . '">' . esc_html( $term->name ) . '</a></li>'; } echo '</ul>';
As a result of executing this code, we get something like this on the screen:
<ul> <li><a href="http://example.com">Term Name 1</a></li> <li><a href="http://example.com">Term Name 2</a></li> </ul>
Changelog
Since 2.3.0 | Introduced. |