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?
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?
foreach
doesn't work like expected withinwhile
loop.