Displaying terms based on loop posts?

I have a shortcode that shows a loop for a custom post type:

while ( $loop->have_posts() ) : $loop->the_post();  
   // do something
endwhile;

Some of the posts are in categories ($term->name), how do I display list of these categories and posts within them based on that loop?

Read More

Example:

I have 2 categories – “Foo” and “Bar”, I have 5 posts in “Foo” and 5 posts in “Bar”.

I’m displaying all the posts and get something like this below them:

Foo (5) | Bar (5)

But in different loop I’m using posts_per_page() limiter, and I’m displaying only 3 posts, not 10, so I want to get:

Foo (3)

I was trying something like this:

while ( $loop->have_posts() ) : $loop->the_post(); 

   $terms = get_the_terms($post->ID, 'portfolio_categories');  

   foreach ( $terms as $term ) {
      $draught_links[] = $term->name;
   }

   var_dump($draught_links);

    // do something
endwhile;

But doing foreach inside of while loop is messy and I’m getting something like

Foo (1) Foo (2) Foo (3) Foo (4) Foo (5) Foo (5) Bar (1) Foo (5) Bar
(2) …

Any ideas?

Related posts

Leave a Reply

1 comment

  1. I think you can try the following options.

    wp_list_categories with the 'show_count' parameter
    http://codex.wordpress.org/Template_Tags/wp_list_categories

    get category and use $count = $category->category_count;
    http://codex.wordpress.org/Function_Reference/get_category

    You can traverse up the functions to get_term if those don’t work and see what is in there, even though you probably can use get the terms you will have to parse the array for output as can be seen in your example, so the above 2 methods would most likely be better.