Reputation: 737
I have a custom hierarchical taxonomy, an example is below:
Filmmaking(?)
--->Production(1)
--->Post-Production (?)
------->VFX (3)
------->Editing(2)
--->Distribution(0)
I want the count next to Filmmaking
be the count of all posts under all its child terms, however nested they be. So in this example it should read 6;
Likewise, Post-production
would read 5
In my current code the count is instead counting how many child terms have posts (so 3 for filmmaking), not how many posts they have .
$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
}
}
The two functions that actually write out the menu are:
//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>';
}
Upvotes: 0
Views: 992
Reputation: 737
Edit: Unhappy with original answer provided I completely redid it in the form of a new function called countDescendentTerms
.
I called this from the two previous functions printIndexNoChildren
and 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');
}
}
I called this from the two previous functions printIndexNoChildren
and 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>';
}
Upvotes: 1