如何在类别页面上显示子类别列表?

时间:2020-08-03 12:46:11

标签: wordpress

有一个类别页面。 如何列出指向其子类别的链接?

<h1>This page category Test</h1>
echo ...; // links all child category for Test

1 个答案:

答案 0 :(得分:0)

首先,您需要获取当前的查看类别ID和分类名称。为此,您需要像下面这样调用get_queried_object()这个函数:

$term = get_queried_object();

然后,您需要调用get_terms()函数以获取如下子类别:

$child_categories = get_terms( $term->taxonomy, array(
    'parent'    => $term->term_id,
    'hide_empty' => false
) );

if ( !empty( $child_categories ) ) { 
    foreach( $child_categories as $c_cat ) {
        echo '<li><a href="' . esc_url( get_term_link( $c_cat, $c_cat->taxonomy ) ) . '">' . $c_cat->name . '</a></li>';
    }
}