计算自定义分类法中多个子术语下的所有帖子
Count all posts under multiple child terms in a custom taxonomy
我有一个自定义的层次分类法,示例如下:
Filmmaking(?)
--->Production(1)
--->Post-Production (?)
------->VFX (3)
------->Editing(2)
--->Distribution(0)
我希望 Filmmaking
旁边的计数是其所有子项下所有 posts 的计数,无论它们是如何嵌套的。所以在这个例子中它应该是 6;
- 生产 1 post
- 视觉特效 3 个帖子
- 正在编辑 2 posts.
同样,Post-production
会读作 5
在我当前的代码中,计数是计算有多少子术语有 posts(所以电影制作是 3),而不是他们有多少 posts。
$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>';
}
我有一个自定义的层次分类法,示例如下:
Filmmaking(?)
--->Production(1)
--->Post-Production (?)
------->VFX (3)
------->Editing(2)
--->Distribution(0)
我希望 Filmmaking
旁边的计数是其所有子项下所有 posts 的计数,无论它们是如何嵌套的。所以在这个例子中它应该是 6;
- 生产 1 post
- 视觉特效 3 个帖子
- 正在编辑 2 posts.
同样,Post-production
会读作 5
在我当前的代码中,计数是计算有多少子术语有 posts(所以电影制作是 3),而不是他们有多少 posts。
$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>';
}