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

时间:2015-01-27 13:08:36

标签: php wordpress

On this SO question我找到了以下内容

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

这是一个步骤,但它没有创建层次结构。我已阅读get categories in 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;
}

1 个答案:

答案 0 :(得分:0)

你必须将它添加到args:

'hierarchical' => 1,

在您的foreach中,您必须检查$ category-&gt; parent是否为空...如果不是空缩进,则添加空格或将类应用于您的li

<li class="indent">