您可以使用get_term_children() 获取术语的子项,但它只返回ID,因此您可以使用get_term_by() 将其转换为id
方法3
此方法基于WP内置的get\\u term\\u children()和\\u get\\u term\\u hierarchy(),但已转换为支持slug。这里提供了整套函数供参考。
类似内置版本,返回unlimited number of children因为它是基于内置的,所以也实现了缓存系统,还添加了缓存清理逻辑,请在生产使用前自由调整和测试。方法3输出不限儿童。如果发现任何错误或不足,请随时发表评论。
// this is based on WP Core, return the immediate 1 level of children
function get_term_children_in_slug( $term_slug, $taxonomy ) {
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( \'invalid_taxonomy\', __( \'Invalid taxonomy.\' ) );
}
$term_data = _get_term_hierarchy_in_slug( $taxonomy );
$terms = $term_data[\'children\'];
$term_slug_ids = $term_data[\'term_slug_ids\'];
if ( ! isset( $term_slug_ids[ $term_slug ] ) ) {
return array();
}
if( ! isset( $terms[ $term_slug_ids[ $term_slug ] ] ) ) {
return array();
}
$children = $terms[ $term_slug_ids[ $term_slug ] ];
// find children of children
foreach ( (array) $terms[ $term_slug_ids[ $term_slug ] ] as $child_slug => $child_id ) {
if ( $term_slug === $child_slug ) {
continue;
}
if ( isset( $terms[ $term_slug_ids[ $child_slug ] ]) ) {
$children = array_merge( $children, get_term_children_in_slug( $child_slug, $taxonomy ) );
}
}
return $children;
}
// this is based on WP Core for get_term_children() but support slug instead
function _get_term_hierarchy_in_slug( $taxonomy ) {
if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
return array();
}
// WP Builtin with cache by saving to option, reduce performance hit
$children = get_option( "{$taxonomy}_children_slug" );
if ( is_array( $children ) ) {
return $children;
}
$children = array();
$terms = get_terms( \'category\', array(
\'hide_empty\' => false,
// \'orderby\' => \'id\', // haven\'t used in test, optional
\'fields\' => \'all_with_object_id\',
) );
$term_ids = []; // as cache to find out terms parent slug
$compare = [];
// all parents become level 1 id => level 2 id structure
foreach ( $terms as $key => $term ) {
$term_ids[ $term->slug ] = $term->term_id;
if ( $term->parent > 0 ) {
// $compare[ $term->slug ][] = $term->parent;
$children[ $term->parent ][$term->slug] = $term->term_id;
}
}
update_option( "{$taxonomy}_children_slug", $children );
// children for use, term ids for matching
return array(
\'children\' => $children,
\'term_slug_ids\' => $term_ids
);
}
// add clean up cache logic together with builtin clean up
add_action( \'clean_taxonomy_cache\', \'custom_clean_up_term_cache\' );
function custom_clean_up_term_cache( $taxonomy ) {
delete_option( "{$taxonomy}_children_slug" );
}
方法2(推荐的方法,因为它是最简单的方法)Asker将第一个答案中的get\\u term\\u by()与get\\u categories()结合使用此方法
取决于args,可以返回直系后代或包括grandchilren在内的所有子代引用posts
// this method only return 1 level of immediate children
function get_term_children_in_slug_approach2( $term_slug ) {
$parent = get_term_by( \'slug\', $term_slug, \'category\' );
$term_slugs = (get_categories([
\'taxonomy\' => \'category\',
\'parent\' => $parent->term_id,
\'hide_empty\' => false, // in the test, have no posts
]));
return $term_slugs;
}
// the following return children and grandchildren
function get_term_children_in_slug_approach2( $term_slug ) {
$parent = get_term_by( \'slug\', $term_slug, \'category\' );
$term_slugs = (get_categories([
\'taxonomy\' => \'category\',
\'child_of\' => $parent->term_id,
\'hide_empty\' => false, // in the test, have no posts
]));
return $term_slugs;
}
方法1因为WP内置工具是基于ID的,所以使用内置get\\u term\\u chilren()并不简单,需要进行ID-to-slug搜索。
返回the first immediate level of children
function getChild ( $slug ) {
$term = get_term_by( \'slug\', $slug, \'category\' );
$term_children = get_term_children( $term->term_id, \'category\' );
// return a list of children id
if( $term_children ) {
return get_term_children( $term->term_id, \'category\' );
}
return $term_children;
}
获得子id列表后,可以将其转换回slug
// helper for retrieve slug from $term ID
function get_term_slug( $id ) {
$term = get_term_by( \'id\', $id, \'category\' );
// var_dump($term);
if( $term ) {
return $term->slug;
}
return $term;
}
// get a list of terms slug from term id
$term_slugs = [];
$term_children_ids = getChild( \'level1\' ); // example
foreach ($term_children_ids as $key => $term_id) {
$term_slugs[] = get_term_slug( $term_id );
}