你需要在$re_child_terms
大堆根据文件get_term_children 返回一个term\\u id数组,因此您需要在id上循环,获取术语并将其保存到新数组中(如果它们不是空的)。
$re_child_term_ids = get_term_children( $term->term_id, \'category\' );
$re_child_terms = array();
foreach ( $re_child_terms_ids as $child_id ) {
$child = get_term($child_id);
if ( is_object($child) && !($child instanceof WP_Error) ) {
if ( $child->count > 0 ) {
$re_child_terms[] = $child;
}
}
}
if ( $term->count > 0 || !empty($re_child_terms) ) {
// do stuff
}
// note: this code is untested but should work
EDIT (封装到函数中):
// in functions.php
if ( ! function_exists ) {
function get_nonempty_term_children( $term_id, $taxonomy ) {
$child_ids = get_term_children( $term_id, $taxonomy );
$child_objs = array();
foreach ($child_ids as $id) {
$child = get_term($id);
if ( is_object($child) && !($child instanceof WP_Error) ) {
if ( $child->count > 0 ) {
$child_objs[] = $child;
}
}
}
return $child_objs;
}
}
// in template
$re_child_terms = get_nonempty_term_children($term->term_id, \'category\');
if ( $term->count > 0 || !empty($re_child_terms) ) {
// do stuff
}