Wordpress 如何按层次结构级别获取术语? 我所拥有的
我有一个名为属性的自定义分类法。Wordpress 如何按层次结构级别获取术语? 我所拥有的,wordpress,hierarchy,levels,taxonomy-terms,Wordpress,Hierarchy,Levels,Taxonomy Terms,我有一个名为属性的自定义分类法。 分类类型是分层的 结构如下: 编辑 听你的评论。如果您知道哪些是哪些,则始终可以使用get\u term\u children() 要实现这种结构,您需要将您的查询看作是一个问题 查询第一级分类术语,然后在处于第一级时继续查询第二级分类术语,依此类推 <?php $taxonomy = 'custom-taxonomy-slug'; $first_level_terms = get_terms( $taxonomy, [ 'parent'
get\u term\u children()
要实现这种结构,您需要将您的查询看作是一个问题 查询第一级分类术语,然后在处于第一级时继续查询第二级分类术语,依此类推
<?php $taxonomy = 'custom-taxonomy-slug';
$first_level_terms = get_terms( $taxonomy, [
'parent' => 0,
'hide_empty' => false,
'title_li' => '',
] );
if ( $first_level_terms ) {
foreach ( $first_level_terms as $first_level_term ) {
$second_level_terms = get_terms( [
'taxonomy' => $taxonomy,
'child_of' => $first_level_term->term_id,
'parent' => $first_level_term->term_id,
'hide_empty' => false,
'title_li' => '',
] );
if ( $second_level_terms ) {
echo '<ul>';
foreach ( $second_level_terms as $second_level_term ) {
echo '<li>' . $second_level_term->slug . '</li>';
};
echo '</ul>';
};
};
}; ?>
此解决方案有两个问题。问题1-对数据库调用大量查询太过广泛。还有第二个——更高级别需要大量的意大利面代码。@AndriiKovalenko更新了。我希望有一个函数可以逐级获取术语,逐级获取术语($level)
。编辑版本更好,但不足以解决问题。
Property 1
Property 2
Property 3
...
Property 1.1
Property 2.1
Property 2.2
Property 3.1
...
Property 1.1.1
Property 2.1.1
Property 2.2.1
Property 3.1.1
...
function get_terms_by_level($level) {
/// ... ?
}
$heirs = get_term_children( 'your-term-slug', $taxonomy );
var_dump( $heirs );
foreach ( $heirs as $key => $value ) {
echo $value;
};
<?php $taxonomy = 'custom-taxonomy-slug';
$first_level_terms = get_terms( $taxonomy, [
'parent' => 0,
'hide_empty' => false,
'title_li' => '',
] );
if ( $first_level_terms ) {
foreach ( $first_level_terms as $first_level_term ) {
$second_level_terms = get_terms( [
'taxonomy' => $taxonomy,
'child_of' => $first_level_term->term_id,
'parent' => $first_level_term->term_id,
'hide_empty' => false,
'title_li' => '',
] );
if ( $second_level_terms ) {
echo '<ul>';
foreach ( $second_level_terms as $second_level_term ) {
echo '<li>' . $second_level_term->slug . '</li>';
};
echo '</ul>';
};
};
}; ?>