Wordpress自定义类别层次结构

时间:2014-12-02 11:41:59

标签: wordpress categories hierarchy

我有这个循环,除了它不以分层方式输出所有类别的事实。相反,它们都按字母顺序列出。

<?php
    $categories = get_categories('hierarchical=1&order=ASC&hide_empty=0');
    foreach ($categories as $cat) { 
    $posts = new WP_Query( array('hierarchical' => 1, 'cat' => $cat->cat_ID));
?>

<div class="project">
    <h2><?php echo $cat->cat_name; ?>/h2>
</div>

<?php
    }
?>

所以目前输出看起来像这样(一个简单的平面字母列表):

Alfa Romeo
Animals
Bugatti
Cars
Cats
Dogs
Mice

...但是我需要它们像这样输出(按字母顺序列表,但是以正确的父/兄弟顺序):

Animals
  Cats
  Dogs
  Mice
Cars
  Alfa Romeo
  Bugatti

我希望它们按字母顺序显示,但也显示父/兄弟关系。

我不需要担心添加一个类或兄弟姐妹或任何东西,(我只是为了演示目的缩进上面的列表)并且平面列表仍然没问题,只要它在正确的位置顺序。

提前致谢。

1 个答案:

答案 0 :(得分:1)

使用wp_list_categories

$args = array(
    'orderby'            => 'name',
    'order'              => 'ASC',
    'hide_empty'         => 0,
    'taxonomy'           => 'category'
    );
 wp_list_categories( $args ); 

<强>更新

$args = array(
'orderby'            => 'name',
'order'              => 'ASC',
'hide_empty'         => 0,
'taxonomy'           => 'category',
'style'              => 'list',
'walker'        =>new My_Category_Walker
);
class My_Category_Walker extends Walker_Category {
    function start_lvl(&$output, $depth=1, $args=array()) {
        $output .= "\n<div class=\"product_cats\">\n";
    }

    function end_lvl(&$output, $depth=0, $args=array()) {
        $output .= "</div>\n";
    }
    function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
        extract($args);
        $cat_name = esc_attr( $category->name );
        $cat_name = apply_filters( 'list_cats', $cat_name, $category );
        $termchildren = get_term_children( $category->term_id, $category->taxonomy );
        if($category->count >0 ){
            $aclass =  ' class="cat_has_posts" ';
        }
        else
        $aclass =  ' class="cat_has_no_posts" ';
        if($category->parent != 0)
            $link = '&nbsp;&nbsp;<a '.$aclass.' href="' . esc_url( get_term_link($category) ) . '" ';
        else
            $link = '<a '.$aclass.' href="' . esc_url( get_term_link($category) ) . '" ';
        if ( $use_desc_for_title == 0 || empty($category->description) )
        $link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
        else
        $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
        $link .= '>';
        $link .= $cat_name . '</a>';
        if ( !empty($show_count) )
        $link .= ' (' . intval($category->count) . ')';
        if ( 'list' == $args['style'] ) {
            $output .= "\t<div";
            $class = 'cat-item cat-item-' . $category->term_id;
            if ( !empty($current_category) ) {
                $_current_category = get_term( $current_category, $category->taxonomy );
                if ( $category->term_id == $current_category )
                $class .=  ' current-cat';
                elseif ( $category->term_id == $_current_category->parent )
                $class .=  ' current-cat-parent';
            }
            $output .=  ' class="' . $class . '"';
            $output .= ">$link\n";
        } else {
            $output .= "\t$link<br />\n";
        }
    }
    function end_el(&$output, $item, $depth=0, $args=array()) {
        $output .= "</div>\n";
    }
}

wp_list_categories( $args );