您可以使用wp_list_categories()功能 http://codex.wordpress.org/Template_Tags/wp_list_categories它使用这些默认值:
<?php wp_list_categories(array(
'show_option_all' => '',
'orderby' => 'name',
'order' => 'ASC',
'style' => 'list',
'show_count' => 0,
'hide_empty' => 1,
'use_desc_for_title' => 1,
'child_of' => 0,
'feed' => '',
'feed_type' => '',
'feed_image' => '',
'exclude' => '',
'exclude_tree' => '',
'include' => '',
'hierarchical' => 1,
'title_li' => __( 'Categories' ),
'show_option_none' => __( 'No categories' ),
'number' => null,
'echo' => 1,
'depth' => 0,
'current_category' => 0,
'pad_counts' => 0,
'taxonomy' => 'category',
'walker' => null
)); ?>
所以从技术上来说你可以说:
wp_list_categories();
这将按层次列出您的类别,隐藏任何空类别,并在所有类别上方添加“类别”标题。
编辑 - 分开父母和孩子
你可以尝试这样的事情,这使用两者的组合wp_list_categories()
和get_categories()功能 http://codex.wordpress.org/Function_Reference/get_categories.
<h1>Categories</h1>
<ul class="blocks">
<?php $parents = get_categories(array('hierarchical' => false));
if(!empty($parents)){
foreach($parents as $parent){
?>
<li>
<h2><?php echo $parent->name; ?></h2>
<ul class="models">
<?php wp_list_categories(array('hierarchical' => false, 'child_of' => $parent->term_id)); ?>
</ul>
</li>
<?php
}
} else {
?>
<li>No Categories</li>
<?php } ?>
</ul>