
站长必藏:26个Linux服务器管理常用SSH命令行
_get_term_children ( $term_id, $terms, $taxonomy, $ancestors = array() )
_get_term_children是一个WordPress函数,它返回一个特定术语的子术语ID数组: 这个函数用于创建分层导航菜单,在那里你需要显示一个类别的列表和它们的父/子关系。它需要两个参数:父术语的ID和分类学名称。
获取$terms的子集,该子集是$term_id的后代。
如果`$terms`是一个对象的数组,那么_get_term_children()返回一个对象的数组。
如果`$terms`是一个ID的数组,那么_get_term_children()返回一个ID的数组。
function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) { $empty_array = array(); if ( empty( $terms ) ) { return $empty_array; } $term_id = (int) $term_id; $term_list = array(); $has_children = _get_term_hierarchy( $taxonomy ); if ( $term_id && ! isset( $has_children[ $term_id ] ) ) { return $empty_array; } // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred. if ( empty( $ancestors ) ) { $ancestors[ $term_id ] = 1; } foreach ( (array) $terms as $term ) { $use_id = false; if ( ! is_object( $term ) ) { $term = get_term( $term, $taxonomy ); if ( is_wp_error( $term ) ) { return $term; } $use_id = true; } // Don't recurse if we've already identified the term as a child - this indicates a loop. if ( isset( $ancestors[ $term->term_id ] ) ) { continue; } if ( (int) $term->parent === $term_id ) { if ( $use_id ) { $term_list[] = $term->term_id; } else { $term_list[] = $term; } if ( ! isset( $has_children[ $term->term_id ] ) ) { continue; } $ancestors[ $term->term_id ] = 1; $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors ); if ( $children ) { $term_list = array_merge( $term_list, $children ); } } } return $term_list; }