WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/get term children

Description

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.

Usage

<?php get_term_children$term$taxonomy ); ?>

Parameters

$term
(string) (required) ID of Term to get children
Default: None
$taxonomy
(string) (required) Taxonomy Name
Default: None

Return Values

(array|WP_Error) 
Array of Term IDs. WP_Error returned if $taxonomy does not exist

Examples

A Basic Example

Used to get an array of children taxonomies and write them out with links in an unordered list.

<?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>';
?> 

This would return something like.

<ul> 
<li><a href="link_to_term_page">Term 1</a></li>
<li><a href="link_to_term_page">Term 2</a></li>
</ul>

Notes

Change Log

Since: 2.3.0

Source File

get_term_children() is located in wp-includes/taxonomy.php.

Related

See also index of Function Reference and index of Template Tags.