2015-03-08 96 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>'; 

就像一个魅力:)