首页 > 解决方案 > 如何显示父类别的子类别

问题描述

如何在页面中获取当前类别的子类别?我们的代码在这里

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$category = get_post_meta( get_the_id(), 'category', true );

print_r( $category); 

标签: wordpress

解决方案


这段代码对我有用。

<?php
    if (is_category()) {
    $this_category = get_category($cat);
    }
    ?>
    <?php
    if($this_category->category_parent)
    $this_category = wp_list_categories('orderby=id&show_count=0
    &title_li=&use_desc_for_title=1&child_of='.$this_category->category_parent.
    "&echo=0"); else
    $this_category = wp_list_categories('orderby=id&depth=1&show_count=0
    &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 } ?>

推荐阅读