我在这里碰壁了,虽然听起来很简单:我想返回一个自定义帖子类型分类术语的层次列表。我得到的是第一级术语和嵌套的ul。但是子术语没有显示。有什么想法吗?
下面是代码:
function return_terms_index() {
$taxonomies = array(
'taxonomy_name',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'fields' => 'all',
'parent' => 0,
'hierarchical' => true,
'child_of' => 0,
'pad_counts' => false,
'cache_domain' => 'core'
);
$terms = get_terms($taxonomies, $args);
$return .= '<ul>';
foreach ( $terms as $term ) {
// return terms (working)
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$term->term_id,
$term->name,
$term->description
);
$subterms = get_terms( array(
'parent' => $term->term_id,
'hide_empty' => false
));
$return .= '<ul>';
foreach ( $subterms as $subterm ) {
//return sub terms (not working :( )
$return .= sprintf(
'<li id="category-%1$s" class="toggle">%2$s <span class="cat-description">%3$s</span>',
$subterm->term_id,
$subterm->name,
$subterm->description
);
$return .= '</li>'; //end subterms li
}
$return .= '</ul>'; //end subterms ul
$return .= '</li>'; //end terms li
} //end foreach term
$return .= '</ul>';
return $return;
}
谢谢你!
**编辑:**以下是输出。
<ul>
<li id="category-176">
1. <span class="post-count">0</span><span class="cat-description" style="display: none;">Description</span>
<ul id="subTerm-176" style="display: block;"></ul>
</li>
<li id="category-49">
2. <span class="post-count">0</span><span class="cat-description" style="display: none;">Langtitel/Beschreibung</span>
<ul id="subTerm-49" style="display: none;"></ul>
</li>
</ul>
**Edit:**现在分类法以分层列表的形式返回,YAY!但是我还想查询和显示第三级分类法术语的帖子,这段代码不起作用。
$post_query = new WP_Query($taxonomies, array(
'term' => $subsubterm->term_id
)); ?>
<?php if ( $post_query->have_posts() ) :
$return .= '<ul>';
while ( $post_query->have_posts() ) : $post_query->the_post();
$return .= '<li><a class="link" href="' . get_permalink() . '">' . get_the_title() . '</a></li>' . "\n";
endwhile;
$return .= '</ul>';
wp_reset_postdata();
else:
endif;
它必须是动态的,所以我不能用名称/鼻涕虫来指定一个术语。
'term' => $subsubterm->term_id
再次感谢。
2条答案
按热度按时间pobjuy321#
您未传入$个分类
请尝试以下代码
zwghvu4y2#
只需使用wp_list_categories()函数即可:https://developer.wordpress.org/reference/functions/wp_list_categories/