Php 仅获取当前类别的第一级子类别
我已经为CTP创建了一个分类归档模板。 在本页中,在列出当前分类法的所有帖子之前,我想列出当前分类法的所有子类别 我设法显示的是这个分类法的所有子类别列表,但我需要过滤掉第二级子类别 例如:Php 仅获取当前类别的第一级子类别,php,wordpress,custom-post-type,custom-taxonomy,Php,Wordpress,Custom Post Type,Custom Taxonomy,我已经为CTP创建了一个分类归档模板。 在本页中,在列出当前分类法的所有帖子之前,我想列出当前分类法的所有子类别 我设法显示的是这个分类法的所有子类别列表,但我需要过滤掉第二级子类别 例如: -分类学 --儿童分类法1 --儿童分类法2 ----儿童的孩子 我只想显示“child-taxonomy1”和“child-taxonomy2” 以下是我目前的代码:使用此代码 <ul> <?php global $post; // grab terms of current post
-分类学
--儿童分类法1
--儿童分类法2
----儿童的孩子 我只想显示“child-taxonomy1”和“child-taxonomy2” 以下是我目前的代码:使用此代码
<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>
$taxonomy_name='productcategory';
$queryed_object=get_queryed_object();
$term\u id=$queryed\u object->term\u id;
$termchildren=get_terms($taxonomy_name,array('parent'=>$term_id,'hide_empty'=>false));
回声“”;
foreach($termchildrenas$child){
回音“”;
}
回声“
”;
工作起来很有魅力:)你看过我的代码了吗?我想进入分类法归档页面,而不是帖子。
$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>';