Wordpress - 类别和嵌套列表子类 [英] Wordpress - Nested list of categories & subcategories
问题描述
我正在尝试显示带有嵌套子类别的 wordpress 类别列表.到目前为止,我只能获得父类别列表或不包括父类别的子类别列表,但我无法将两者结合在一起.
Im trying to display a list of my wordpress categories with nested subcategories. So far i've only been able to get a list of parent categories or a list of subcategories excluding parents but I haven't been able to join the two together.
这是我希望创建的结果:
This is the sort of result I am looking to create:
- 父类别
- 子类别
- 子类别
- 子类别
- 子类别
- 子类别
- 子类别
这样做的想法是创建一个自定义类别页面.HTML如下:
The idea for this is to create a custom category page. The HTML is as follows:
<h1>Categories</h1> <ul class="blocks"> <li> <img src="http://placehold.it/250x250" alt="title" /> <h2>Parent Category</h2> <ul class="models"> <li><a href="#">Sub Category</a></li> <li><a href="#">Sub Category</a></li> </ul> </li> <li> <img src="http://placehold.it/250x250" alt="title" /> <h2>Parent Category</h2> <ul class="models"> <li><a href="#">Sub Category</a></li> <li><a href="#">Sub Category</a></li> <li><a href="#">Sub Category</a></li> </ul> </li> </ul>
推荐答案
您可以使用
wp_list_categories()
函数 使用这些默认值:You can use
wp_list_categories()
function which uses these defaults:<?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 )); ?>
所以从技术上讲,您可以说:
So technically you can just say:
wp_list_categories();
这将按层次列出您的类别,隐藏任何空类别,并在所有类别上方添加类别"标题.
And this will list your categories hierarchically hiding any empty categories and adding a title of "Categories" above them all.
编辑 - 分开的父母和孩子
您可以尝试这样的操作,使用
wp_list_categories()
和get_categories()
函数.You can try something like this, this using a combination of both
wp_list_categories()
and theget_categories()
function.<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>
这篇关于Wordpress - 类别和嵌套列表子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!