首页 > 解决方案 > 计算自定义分类中多个子术语下的所有帖子

问题描述

我有一个自定义的分层分类法,一个例子如下:

Filmmaking(?)
--->Production(1)
--->Post-Production (?)
------->VFX (3)
------->Editing(2)
--->Distribution(0)

我希望接下来Filmmaking的计数是其所有子术语下所有帖子的计数,无论它们是嵌套的。所以在这个例子中它应该是6

同样,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>';
}

标签: wordpresscustom-taxonomy

解决方案


编辑:对原始答案不满意,前提是我以名为countDescendentTerms.

我从前面的两个函数printIndexNoChildrenprintIndexHasChildren.

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');
   }  

}

我从前面的两个函数printIndexNoChildrenprintIndexHasChildren.

//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>';   
}

推荐阅读