我的Wordpress网站中有三个级别的类别。 看起来像是:
Parent A
- Child A1
- - Child child A11
- - Child child A12
- Child A2
- - Child child A21
- - Child child A22
Parent B
- Child B1
- - Child child B11
- - Child child B12
- Child B2
- - Child child B11
- - Child child B12
我有一个代码:
<?php
$termID = 3; // Parent A ID
$taxonomyName = "products";
$termchildren = get_term_children( $termID, $taxonomyName );
echo'<div class="sub"> <div class="links">';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo'<a href="#">' . $term->name . '</a>';}
echo'</div> </div>';?>
代码的结果是:
如何只让没有孩子的孩子?
答案 0 :(得分:10)
你可以尝试
$args = array('child_of' => $termID, 'parent' => $termID);
$termchildren = get_terms( $taxonomyName, $args);
而不是
$termchildren = get_term_children( $termID, $taxonomyName );
如果child_of
和parent
值相同,则结果只有一个级别。