Wordpress自定义分类法-如果不是父项的话

问题描述 投票:0回答:1

我有两个级别的术语的自定义分类法。

  • 父项
    • 子项
    • 子项
  • 父项
    • 子项
    • 子项
  • 父项(没有孩子)

我正在将自定义archive.php模板与一些HTML一起使用,而这些HTML只想显示在术语[[无子术语上。

这是我尝试过的...

$taxonomy = 'custom_tax'; $term = get_queried_object(); $children = get_terms( $term->taxonomy, array( 'parent' => $term->term_id ) ); if(!$children) { echo '<p>HTML only terms without child terms</p>'; }

如果这是子项,但在没有子项的父项中,则无效!

请帮助吗?

wordpress custom-post-type custom-taxonomy
1个回答
0
投票
parent参数中将0设置为get_terms,将仅返回顶级分类法术语:

$terms = get_terms( array( 'taxonomy' => 'custom_tax', 'parent' => 0 ) );

然后,您可以根据是否有子项来筛选结果。
© www.soinside.com 2019 - 2024. All rights reserved.