2014-12-02 132 views
2

我已经有了这个循环,除非事实上它不会以分层方式输出所有类别。相反,它们全部按字母顺序排列。WordPress的自定义类别层次结构

<?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

使用wp_list_categories

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

UPDATE

$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); 
+0

这只会吐出一个无序列表,这是不是我想要的目的。正如你可以在代码示例中看到的,我正在使用div。代码示例显示为简化,但实际上我也会在div中添加很多其他标记。 – user3256143 2014-12-02 11:59:35

+0

pl检查更新代码 – 2014-12-02 13:19:32

+0

Bravo。这是完美的。谢谢。 – user3256143 2014-12-02 21:53:12