列出当前类别顶级父级的类别 - Wordpress

时间:2014-07-28 08:12:48

标签: wordpress

我试图列出Wordpres中当前类别的顶级父级的类别。这是我目前的代码:

$currentcat = get_query_var('cat');
$cats_str = get_category_parents($cat, false, '%#%');
$cats_array = explode('%#%', $cats_str);
$cat_depth = sizeof($cats_array)-2;

<?php wp_list_categories('orderby=id&show_count=0&title_li=&use_desc_for_title=0&child_of='.$currentcat); ?>

但是,这仅列出当前类别的类别,我想在当前类别的顶级下方显示所有类别。感谢

1 个答案:

答案 0 :(得分:0)

试试这个:

$term =  $wp_query->queried_object;
$term_id = $term->term_id;
$taxonomy_name = 'your_taxonomy';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo '<ul>';
foreach ( $termchildren as $child ) {
   $term = get_term_by( 'id', $child, $taxonomy_name );
   echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name .   '</a></li>';
}
echo '</ul>';