函数原型
get_term_children( int $term_id, string $taxonomy ): array|WP_Error
函数描述
Merges all term children into a single array of their IDs.
是否弃用
未弃用
函数参数
-
$term_id
int
Required - ID of term to get children.
-
$taxonomy
string
Required - Taxonomy name.
函数返回值
array|WP_Error List of term IDs. WP_Error returned if $taxonomy does not exist.
函数位置
File: wp-includes/taxonomy.php.
函数源码
function get_term_children( $term_id, $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
}
$term_id = (int) $term_id;
$terms = _get_term_hierarchy( $taxonomy );
if ( ! isset( $terms[ $term_id ] ) ) {
return array();
}
$children = $terms[ $term_id ];
foreach ( (array) $terms[ $term_id ] as $child ) {
if ( $term_id === $child ) {
continue;
}
if ( isset( $terms[ $child ] ) ) {
$children = array_merge( $children, get_term_children( $child, $taxonomy ) );
}
}
return $children;
}
源码链接
变更日志
| Version | Description |
|---|---|
| 2.3.0 | Introduced. |

