get_term_children()
get_term_children( int $term_id, string $taxonomy ) 将所有…
get_term_children( int $term_id, string $taxonomy )
将所有术语子项合并到其ID的单个数组中。
Merge all term children into a single array of their IDs.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
这个递归函数将把$term的所有子项合并到同一个term id数组中。仅适用于分级的分类法。
如果$term在$taxonomy中不存在,则返回空数组。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $term_id | (int) | 必需 | 获取孩子的术语ID。 |
| $taxonomy | (string) | 必需 | 分类法名称。 |
返回(Return)
(array|WP_Error)术语ID列表。如果$taxonomy不存在,则返回WP_Error。
源码(Source)
/**
* 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.
*
* @since 2.3.0
*
* @param string $term_id ID of Term to get children.
* @param string $taxonomy Taxonomy Name.
* @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
*/
function get_term_children( $term_id, $taxonomy ) {
if ( ! taxonomy_exists($taxonomy) )
return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
$term_id = intval( $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;
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 2.3.0 | wp-includes/taxonomy.php:1005 | 3 | 5 |
笔记(Notes)
一个基本的例子
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!