如何计算WordPress中每个父术语下的子术语数量



我想统计WordPress中每个父项下的子项数量。

我在WordPress中创建了一个自定义分类法。我想在自定义页面上显示这个自定义分类法的所有术语,例如:

1.我想在循环中显示每个父项下的所有子项
2。我想计算每个父术语下的子术语的数量

每个学期的帖子数量正在统计中。但我对这个术语有意见。

这是我的密码。

<?php 
$args = array(
'taxonomy' => 'pharma',
'get' => 'all',
'parent' => 0,
'hide_empty' => 0
);
$terms = get_terms( $args );
foreach ( $terms as $term ) : ?>
<div class="single_pharma">
<h2 class="pharma_name"><a href="<?php echo esc_url( get_term_link( $term ) ); ?>"><?php echo $term->name; ?></a></h2>
<span class="count_category"><span>Generics:</span><?php // want to display here sub term count  ?></span>
<span class="count_brand"><span>Brands:</span><?php echo $term->count; ?></span>
</div>

您可以使用get_term_childrenDocs函数来获取所有的"sub_terms":

$args = array(
'taxonomy'   => 'pharma',
'get'        => 'all',
'parent'     => 0,
'hide_empty' => 0
);
$terms = get_terms($args);
foreach ($terms as $term) {
$count_sub_terms = count(get_term_children($term->term_id, 'pharma'));
?>
<div class="single_pharma">
<h2 class="pharma_name"><a href="<?php echo esc_url(get_term_link($term)); ?>"><?php echo $term->name; ?></a></h2>
<span class="count_category"><span>Generics:</span><?php echo $count_sub_terms;  ?></span>
<span class="count_brand"><span>Brands:</span><?php echo $term->count; ?></span>
</div>
<?php
}

最新更新