如何使用自定义标记和层次结构输出类别


How to output categories with custom markup and hierarchy?

在这个SO问题上,我发现了以下内容

$args = array(
 'orderby' => 'name',
 'order' => 'ASC',
 'number' => 20 // how many categories
);
$categories = get_categories($args);
foreach($categories as $category) { 
  echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>&rsaquo;';
} 

这是一个步骤,但它没有创建层次结构。我已经阅读了 wp codex 中的获取类别,但我无法解决。

我的结构是:

CAT 1
  cat 1.1
  cat 1.2
CAT 2
  cat 2.1
  cat 2.2

试过这个,但输出只是一个根本没有层次结构的项目列表:

public function walk_taxonomy( $type = "checkbox", $args = array() ) {
    $args['walker'] = new Search_Filter_Taxonomy_Walker($type, $args['name']);
    $output = $argo = array(
        'hierarchical' => 1
        );
        $categories = get_categories($argo);
        foreach($categories as $category) { 
            echo '<li><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></li>&rsaquo;';
        } 
    if ( $output )
        return $output;
}

你必须把它添加到参数中:

'hierarchical' => 1,

在你的 foreach 中,你必须检查$category->父级是否为空.. 如果不是空缩进,则通过添加空格或将类应用于您的 li 喜欢

<li class="indent">