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

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

问题描述:

On this SO question I have found the following

$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;';
} 

That's a step but that it isn't creating a hierarchy. I have read get categories in wp codex but I couldn't work it out.

My structure is:

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

Tried this but the output is simply a list of items with no hierarchy at all:

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;
}

you have to add this to the args:

'hierarchical' => 1,

in your foreach you have to check if $category->parent is empty or not.. if not empty indent by adding spaces or applying a class to your li like

<li class="indent">