我想計算 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>

uj5u.com熱心網友回復:
您可以使用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
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/396412.html
標籤:php WordPress的 wordpress 主题 自定义wordpress-pages 分类术语
上一篇:如何在flutter中將json主體發布到phpapi?
下一篇:帶有php的FTP下載檔案夾
