需要帮助:我必须只显示自定义分类WordPress的特定父级的子条款,在我的例子中:分类名称:“region”,与此相关:父条款及其子级: 欧洲: - 葡萄牙; - 德国; - 英格兰;
亚: - 中国; - 日本;
所以例如我需要在列表中只显示欧洲的孩子,我该怎么做?我尝试了很多方法,只能显示所有父母的所有孩子:
<?php
$taxonomyName = "region";
//This gets top layer terms only. This is done by setting parent to 0.
$parent_terms = get_terms( $taxonomyName, array( 'parent' => 0, 'orderby' => 'slug', 'hide_empty' => false ) );
echo '<ul>';
foreach ( $parent_terms as $pterm ) {
//Get the Child terms
$terms = get_terms( $taxonomyName, array( 'parent' => $pterm->term_id, 'orderby' => 'slug', 'hide_empty' => false ) );
foreach ( $terms as $term ) {
echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
}
}
echo '</ul>';
?>
但我只需要为特定的父母显示。谢谢你的帮助
答案 0 :(得分:0)
你已经有了答案。只需设置您的父术语并删除顶部嵌套的foreach。
<?php
$taxonomyName = "region";
//Could use ACF or basic custom field to get the "parent tax ID" dynamically from a page. At least that's what I would do.
$parent_tax_ID = '3';
$parent_tax = get_term($parent_tax_ID);
echo '<h3>' . $parent_tax->name . '</h3>';
echo '<ul>';
$terms = get_terms( $taxonomyName, array( 'parent' => $parent_tax_ID, 'orderby' => 'slug', 'hide_empty' => false ) );
foreach ( $terms as $term ) {
echo '<li><a href="' . get_term_link( $term ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
?>