get_category_children()
云策文档标注
概述
get_category_children() 是一个已弃用的 WordPress 函数,用于检索指定分类 ID 的子分类列表,并以字符串形式返回,子分类 ID 前后可添加分隔符。自 WordPress 2.8.0 起,建议使用 get_term_children() 替代。
关键要点
- 函数已弃用:自 WordPress 2.8.0 起,get_category_children() 被标记为弃用,推荐使用 get_term_children()。
- 功能描述:该函数递归检索指定分类 ID 的所有子分类,返回一个字符串,其中子分类 ID 前后可自定义添加分隔符(如 '/')。
- 参数说明:接受 $id(分类 ID,必填)、$before(前置分隔符,默认 '/')、$after(后置分隔符,默认空字符串)和 $visited(已访问分类 ID 数组,默认空数组)。
- 返回值:返回字符串形式的子分类 ID 链,若 $id 为 0 或出错则返回空字符串或 WP_Error 对象。
- 替代函数:get_term_children() 返回数组形式的子分类 ID,需指定分类法参数为 'category',注意返回值类型差异。
代码示例
function get_category_children( $id, $before = '/', $after = '', $visited = array() ) {
_deprecated_function( __FUNCTION__, '2.8.0', 'get_term_children()' );
if ( 0 == $id )
return '';
$chain = '';
/** TODO: Consult hierarchy */
$cat_ids = get_all_category_ids();
foreach ( (array) $cat_ids as $cat_id ) {
if ( $cat_id == $id )
continue;
$category = get_category( $cat_id );
if ( is_wp_error( $category ) )
return $category;
if ( $category->parent == $id && !in_array( $category->term_id, $visited ) ) {
$visited[] = $category->term_id;
$chain .= $before.$category->term_id.$after;
$chain .= get_category_children( $category->term_id, $before, $after );
}
}
return $chain;
}注意事项
- 弃用警告:使用此函数会触发 _deprecated_function() 警告,建议在开发中避免使用,改用 get_term_children()。
- 返回值差异:get_category_children() 返回字符串,而 get_term_children() 返回数组,迁移时需调整代码处理逻辑。
- 性能考虑:函数内部调用 get_all_category_ids()(也已弃用),可能影响性能,尤其是在分类数量多时。
- 递归机制:函数使用递归遍历子分类,注意 $visited 参数用于防止无限循环。
原文内容
Retrieve category children list separated before and after the term IDs.
Description
See also
Parameters
$idintrequired-
Category ID to retrieve children.
$beforestringoptional-
Prepend before category term ID. Default
'/'. $afterstringoptional-
Append after category term ID. Default empty string.
$visitedarrayoptional-
Category Term IDs that have already been added.
Default:
array()
Source
function get_category_children( $id, $before = '/', $after = '', $visited = array() ) {
_deprecated_function( __FUNCTION__, '2.8.0', 'get_term_children()' );
if ( 0 == $id )
return '';
$chain = '';
/** TODO: Consult hierarchy */
$cat_ids = get_all_category_ids();
foreach ( (array) $cat_ids as $cat_id ) {
if ( $cat_id == $id )
continue;
$category = get_category( $cat_id );
if ( is_wp_error( $category ) )
return $category;
if ( $category->parent == $id && !in_array( $category->term_id, $visited ) ) {
$visited[] = $category->term_id;
$chain .= $before.$category->term_id.$after;
$chain .= get_category_children( $category->term_id, $before, $after );
}
}
return $chain;
}
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Deprecated. Use get_term_children() |
| 1.2.0 | Introduced. |
Skip to note 2 content
jon
If attempting to replace this deprecated function with get_term_children() , note that get_category_children() returns a String, while get_term_children() returns an array of Category IDs. Also note that get_term_children() requires a second parameter of ‘category’, which is the name of the pre-defined Taxonomy for Categories.