Wordpress自定义类别层次结构

Wordpress自定义类别层次结构,wordpress,categories,hierarchy,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' => $ca

我有一个循环,除了它没有以分层的方式输出所有类别之外,它是有效的。相反,它们都是按字母顺序排列的

<?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
    }
?>
…但我需要它们像这样输出(按字母顺序排列的列表,但顺序正确):

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

我不需要担心添加一个类或兄弟类或任何东西(我缩进了上面的列表只是为了演示),而平面列表仍然可以,只要顺序正确

提前感谢。

使用wp\u列表\u类别

$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 );
$args=array(
'orderby'=>'name',
“订单”=>“ASC”,
“hide_empty”=>0,
“分类法”=>“类别”,
'样式'=>'列表',
“walker”=>新的My_类别\u walker
);
类My_Category_Walker扩展了Walker_Category{
函数start_lvl(&$output,$depth=1,$args=array()){
$output.=“\n\n”;
}
函数end_lvl(&$output,$depth=0,$args=array()){
$output.=“\n”;
}
函数start_el(&$output,$category,$depth=0,$args=array(),$id=0){
摘录($args);
$cat\u name=esc\u attr($category->name);
$cat\u name=应用过滤器($list\u cats',$cat\u name,$category);
$termchildren=get_term_children($category->term_id,$category->taxonomy);
如果($category->count>0){
$aclass='class=“cat_有_帖子”';
}
其他的
$aclass='class=“cat_没有帖子”';
如果($category->parent!=0)
$link='';
如果(!空($show_count))
$link.='('.intval($category->count)。');
如果('list'=$args['style']){
$output.=“\t$link\n”;
}否则{
$output.=“\t$link
\n”; } } 函数end_el(&$output,$item,$depth=0,$args=array()){ $output.=“\n”; } } wp_列表_类别($args);
这只会产生一个无序的列表,这不是我想要实现的。正如您在代码示例中看到的,我正在使用divs。代码示例显示得很简单,但实际上我也将在div中添加许多其他标记。
$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 );