dongzao3923 2014-05-14 13:58
浏览 109
已采纳

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:

  • Parent Category
    • Subcategory
    • Subcategory
  • Parent Category
    • Subcategory
  • Parent Category
    • Subcategory
    • Subcategory
    • Subcategory

EDIT:

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>
  • 写回答

2条回答 默认 最新

  • dsfd3546 2014-05-14 14:20
    关注

    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.

    EDIT - Separate Parents and Children

    You can try something like this, this using a combination of both wp_list_categories() and the get_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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab