2017-12-18 118 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>'; 
?> 
相關問題