wordpress - 计算自定义分类中多个子术语下的所有帖子
问题描述
我有一个自定义的分层分类法,一个例子如下:
Filmmaking(?)
--->Production(1)
--->Post-Production (?)
------->VFX (3)
------->Editing(2)
--->Distribution(0)
我希望接下来Filmmaking
的计数是其所有子术语下所有帖子的计数,无论它们是嵌套的。所以在这个例子中它应该是6;
- 生产 1 个帖子
- 视觉特效3 个帖子
- 编辑2个帖子。
同样,Post-production
将读取5
在我当前的代码中,计数是计算有多少子术语有帖子(所以 3 用于电影制作),而不是他们有多少帖子。
$getParents = [
'taxonomy' => $taxonomy,
'parent' => 0,
'number' => 0,
'hide_empty' => false
];
$parent_terms = get_terms( $getParents );
foreach ( $parent_terms as $parent_term ) {
if ( hasChildren($parent_term) ) {
printIndexHasChildren( $parent_term);
echo '<ul>';
echo '<li><a id="'.$parent_term->slug.'" href="'. get_term_link( $parent_term ) .'"> All '. $parent_term->name.'</a></li>';
//Loop through child items and output name
foreach ( get_terms( 'kernal_category', array( 'hide_empty' => false, 'parent' => $parent_term->term_id ) ) as $child_term) {
if ( hasChildren( $child_term ) ) {
printIndexHasChildren( $child_term);
echo '<ul>';
//If child has second child
foreach ( get_terms( 'kernal_category', array( 'hide_empty' => false, 'parent' => $child_term->term_id ) ) as $second_child_term) {
printIndexNoChildren( $second_child_term );
}
echo '</ul>';
echo '</li>';
} else {
printIndexNoChildren( $child_term );
}
}
echo '</ul>';
echo '</li>';
} else {
printIndexNoChildren( $parent_term);
}
}
} else {
... do something
}
}
实际写出菜单的两个函数是:
//Printing Independent List Elements
function printIndexNoChildren($term) {
echo '<li><a id="'.$term->slug.'" href="'. get_term_link( $term ) .'">'. $term->name.' ('.$term->count.')</a></li>';
}
//Print List Elements With Children
function printIndexHasChildren($term) {
echo '<li><a id="'.$term->slug.'" href="'. get_term_link( $term ) .'">'. $term->name.' ('.$term->count.')</a>';
}
解决方案
编辑:对原始答案不满意,前提是我以名为countDescendentTerms
.
我从前面的两个函数printIndexNoChildren
和printIndexHasChildren
.
function countDescendentTerms($term) {
if (!$term)
return false;
//Define Arguments to Send to get_terms
$getTerms = [
'taxonomy' => 'kernal_category',
'parent' => $term->term_id,
'hide_empty' => false
];
$descendentTerms = get_terms( 'kernal_category', $getTerms );
if( !is_wp_error($descendentTerms)) {
$parentCountX = $term->count;
foreach ( $descendentTerms as $firstChildTermX ) {
if ( hasChildren($firstChildTermX) ) {
foreach ( get_terms( 'kernal_category', array( 'hide_empty' => false, 'parent' => $firstChildTermX->term_id ) ) as $secondChildTermX ) {
if ( hasChildren( $secondChildTermX ) ) {
foreach ( get_terms( 'kernal_category', array( 'hide_empty' => false, 'parent' => $secondChildTermX->term_id ) ) as $thirdChildTermX ) {
$thirdChildCountX += $thirdChildTermX->count;
}
}
$secondChildCountX += $secondChildTermX->count;
}
$totalThirdChildCountX += $thirdChildCountX;
$thirdChildCountX = 0;
}
$firstChildCountX += $firstChildTermX->count;
}
$totalPostCountx = $parentCountX + $firstChildCountX + $secondChildCountX + $totalThirdChildCountX;
$secondChildCountX = 0;
return $totalPostCountx;
} else {
print_r('Error');
}
}
我从前面的两个函数printIndexNoChildren
和printIndexHasChildren
.
//Printing Independent List Elements
function printIndexNoChildren($term) {
$postCountX = countDescendentTerms( $term );
echo '<li><a id="'.$term->slug.'" href="'. get_term_link( $term ) .'">'. $term->name.' ('.$postCountX.')</a></li>';
}
//Print List Elements With Children
function printIndexHasChildren($term, $array = null) {
$postCountX = countDescendentTerms( $term );
echo '<li><a id="'.$term->slug.'" href="'. get_term_link( $term ) .'">'. $term->name.' ('.$postCountX.')</a>';
}
推荐阅读
- python - Valueerror y 应该是一个一维数组,得到一个形状为 (14630, 3) 的数组。从 vgg16 中提取特征然后放入 svm
- javascript - Angular 10+ 复选框过滤
- c# - 使用 Google API 的 C# Speech To Text
- powershell - 如何使用 Import-excel Powershell 从所有标题中删除“A_”
- node.js - ENOENT:没有这样的文件或目录,需要运行 docker 和 nodejs 时打开 'C:\Users\username\package.json'
- amazon-ec2 - 无法确定代理 ARP 的以太网地址(Cent OS PPTP VPN)
- python - 错误:fit() 得到了一个意外的关键字参数“nb_epoch”
- javascript - scrollIntoView() 仅在 React 中的 div 中滚动
- haskell - 如何链接 cabal 库和可执行文件?
- spring-boot - liquibase - 尝试调用不存在的方法