我想只显示第一级子分类,下面是我的代码,它只是不起作用,任何人都可以告诉我的代码有什么问题?非常感谢。
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array('child_of' => $term_id, 'parent' => $term_id);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach ( $termchildren as $child ) {
$term = get_term_by( 'id', $child, $taxonomy_name );
echo '<li><a href="#">' . $term->name . '</a></li>';
}
echo '</ul>';
?>
答案 0 :(得分:0)
添加hide_empty
。
<?php
$term_id = 31;
$taxonomy_name = 'kosher_category';
$args = array(
'child_of' => $term_id,
'parent' => $term_id,
'hide_empty' => false // ADDED
);
$termchildren = get_terms($taxonomy_name, $args );
echo '<ul>';
foreach($termchildren AS $child) {
$term = get_term_by('id', $child, $taxonomy_name);
echo '<li><a href="#">' . $term->name . '</a></li>';
}
echo '</ul>';
?>