2017-12-18 121 views
0

需要帮助:我必须只显示自定义分类标准WordPress的特定父项的子项,在我的情况下:分类标准名称:“region”,与此有关:父母条款及其子女: 欧洲: - 葡萄牙; - 德国; - 英格兰;想要只显示自定义分类标准的特定父项的子项WordPress

亚洲: - China;日本; - 日本;

因此,例如我需要在列表中只显示欧洲的儿童,我该如何做到这一点?我试了很多方法,只能显示所有父母的所有孩子:

 <?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

你已经有了答案。只需设置你的父项,并摆脱顶层嵌套的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>'; 
?> 
相关问题