2015-03-08 91 views
0

我已爲CTP創建了分類歸檔模板。 在此頁面中,列出當前分類的所有帖子之前 - 我想列出當前分類的所有子類別。僅獲取當前類別的第一級子類別

我設法顯示的是這個分類的子類別的整個列表,但我需要篩選出第二級別的孩子。

例如:
物種分類數據庫
--child-taxonomy1
--child-taxonomy2
----兒童taxonomy2的孩子

我想只顯示 「兒童taxonomy1」和 「兒童taxonomy2」

這裏是我到目前爲止的代碼:http://www.codeshare.io/MQ4YT

回答

0

使用此代碼

<ul> 
<?php 
global $post; 
// grab terms of current post, replace taxonomy name 
$terms = get_the_terms($post->ID, 'name_of_custom_taxonomy'); 
// define arguments of following listing function 
$args = array (
    'child_of' => $terms[0], // current post's (first) category 
    'title_li' => '', // disable display of outer list item 
    'taxonomy' => 'name_of_custom_taxonomy' // replace as well 
); 
// list child categories 
wp_list_categories($args); 
?> 
</ul> 
+0

你看看我的代碼?我試圖進入分類歸檔頁面,而不是在帖子中。 – gargi 2015-03-08 20:58:06

0
$taxonomy_name = 'product-category'; 
      $queried_object = get_queried_object(); 
      $term_id = $queried_object->term_id; 

      $termchildren = get_terms($taxonomy_name, array('parent' => $term_id, 'hide_empty' => false)); 

      echo '<ul>'; 
      foreach ($termchildren as $child) { 
       echo '<li><a href="' . get_term_link($child, $taxonomy_name) . '">' . $child->name . '</a></li>'; 
      } 
      echo '</ul>'; 

就像一個魅力:)