首页 > 解决方案 > 如何在所有页面上显示所有子类别

问题描述

我正在尝试显示包含当前父类别的所有子类别的菜单,但不显示父类别。看起来很简单,但我无法删除父名称,或在单个帖子中显示子类别。

这是我目前正在使用的代码

<?php if (is_front_page() or is_single() ) { ?>

        <?php } else { ?>

             <?php
             if (is_category() ) {
                $this_category = get_category($cat);
             }
             ?>
             <?php
             if($this_category->category_parent)
                $this_category = wp_list_categories('orderby=id&title_li=&use_desc_for_title=1');

                else
                $this_category = wp_list_categories('orderby=id&depth=1&title_li=&use_desc_for_title=1&child_of='.$this_category->cat_ID."&echo=0");
                    if ($this_category) { ?> 
                    <ul>
                    <?php echo $this_category; ?>
                    </ul>

                <?php } ?>



<?php } ?>

标签: wordpress

解决方案


您可以在要显示子类别的位置使用以下代码

$args = array('child_of' => $this_category->cat_ID);
$categories = get_categories( $args );

echo "<ul>";
foreach($categories as $category) { 
    echo "<li>".$category->name."</li>";
}
echo "</ul>"

经过测试,效果很好。


推荐阅读