计算子自定义分类中的帖子数

时间:2015-12-10 00:50:34

标签: php wordpress

我有这段代码:

 <?php
$taxonomyName = "free";
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
    $term_children = get_term_children($term->term_id,$taxonomyName);
    echo '<ul>';
    foreach($term_children as $term_child_id) {
        $term_child = get_term_by('id',$term_child_id,$taxonomyName);
        echo '<li><a href="' . get_term_link( $term_child->term_id, $taxonomyName ) . '">' . $taxtitle . ' ' . $term_child->name . '</a></li>';
    }
    echo '</ul>';
}
?>

我想计算这个父分类法的每个子分类法的帖子。

我想得到这样的结果:

echo '<li><a href="' . get_term_link( $term_child->term_id, $taxonomyName ) . '">' . $taxtitle . ' ' . $term_child->name . '</a> (COUNT - Episodes)</li>';

谢谢。

1 个答案:

答案 0 :(得分:-1)

应该相当简单:

<?php
$taxonomyName = "free";
$terms = get_terms($taxonomyName,array('parent' => 0));
foreach($terms as $term) {
    $term_children = get_term_children($term->term_id,$taxonomyName);
    echo '<ul>';
    foreach($term_children as $term_child_id) {
        $term_child = get_term_by('id',$term_child_id,$taxonomyName);
        $q = new WP_Query(array(
            'nopaging' => true,
            'tax_query' => array(
              'taxonomy' => 'category',
              'field' => 'id',
              'terms' => $term_child_id,
              'include_children' => true,
          )));
         $count = $q->post_count;
        echo '<li><a href="' . get_term_link( $term_child->term_id, $taxonomyName ) . '">' . $taxtitle . ' ' . $term_child->name . '</a>(Season-'.$count.')</li>';          
    }
    echo '</ul>';
}
?>