我相信你能找到accordion 任意位置的代码示例。转换可能会有帮助terms 转换为更有利于嵌套元素的结构。
<?php
// Get terms as nested array
function get_nested_terms ($taxonomy, $array=array(), $args=array() ) {
$args = wp_parse_args($args, array(
\'orderby\' => \'count\',
\'parent\' => 0,
\'hide_empty\' => true
));
$terms = get_terms($taxonomy, $args);
foreach ( $terms as $term ) {
$args[\'parent\'] = $term->term_id;
$children = get_nested_terms ( $taxonomy, array(), $args);
$array [$term->term_id] = array(
\'term\' => $term,
\'children\' => $children
);
}
return $array;
}
// Render HTML from nested terms array
function render_nested_terms($array, $level = 0){
$level++;
foreach ( $array as $term_info ) {
$classes = array(
"level-" . $level,
empty($term_info[\'children\'] ) ? \'no-children\' : \'has-children\'
);
echo \'<ul class="\' . implode (\' \', $classes ) . \'" ><li>\';
// Showing the term name,
// but you could include term content below this item
echo "term: " . $term_info[\'term\']->name;
// Render the nested items
if( ! empty($term_info[\'children\'] )) {
render_nested_terms ( $term_info[\'children\'], $level );
}
echo \'</li></ul>\';
}
}
首先,我们将运行一个函数来获取所有
terms 您要使用。设置
hide_empty
若为true,则仅返回包含与之关联的内容的类别。
$options = array(
\'parent\' => 0,
\'hide_empty\' => false
);
$nested_terms = get_nested_terms (\'category\', array(), $options );
接下来,循环遍历嵌套元素并呈现HTML内容。
render_nested_terms ($nested_terms);
此时,您需要根据嵌套元素的架构添加JavaScript或其他内容。