可以使用以下函数递归创建层次结构:
function sort_hierarchical(array &$cats, array &$into, $parent_id = 0){
foreach ($cats as $i => $cat) {
if ($cat->parent == $parent_id) {
$into[$cat->term_id] = $cat;
unset($cats[$i]);
}
}
foreach ($into as $top_cat) {
$top_cat->children = array();
sort_hierarchical($cats, $top_cat->children, $top_cat->term_id);
}
}
使用方法如下:
$terms = get_terms(array(\'taxonomy\' => \'category\', \'hide_empty\' => false));
$cats = array();
sort_hierarchical($terms,$cats) ;