Support » Plugins » Get Number Of Posts Per Category?

  • Hi all,

    I have generated a category list and under each category name it displays the posts in an accordion style menu. Although I’m struggling to get my listing to show the number of posts within each category, ie

    Category Name (4)
    – post 1
    – post 2
    – post 3
    – post 4

    Can anyone help me on this one? I’ve pasted my code below.
    Any help would be great.

    <?php
    //get all categories then display all posts in each term
    $taxonomy = 'category';
    $show_count   = 1;      // 1 for yes, 0 for no
    $param_type = 'category__in';
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
    
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1,
          'show_count'         => 1,
    
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {  ?>
    
    	    <h3><a href="#"><?php echo $term->name ?></a></h3>
    <div>
    	    <?php
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
           <?php
          endwhile;
          ?>
          </div>
    
     <?php
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  • The topic ‘Get Number Of Posts Per Category?’ is closed to new replies.