#1
You can use the get_posts()
function to retrieve the custom post type movies associated with each actor. Then, you can count the number of posts with the count()
function.
<?php foreach (get_terms('actor') as $cat) :
$args = array(
'post_type' => 'movie',
'tax_query' => array(
array(
'taxonomy' => 'actor',
'field' => 'slug',
'terms' => $cat->slug
)
)
);
$movies = get_posts( $args );
$num_movies = count( $movies );
?>
<div class="col-md-4">
<a href="/wordpress/actor/<?php echo $cat->name; ?>"><?php echo $cat->name; ?> (<?php echo $num_movies; ?>)</a><br />
</div>
<?php endforeach; ?>
#2
To hyperlink it to taxonomy-actor.php, you can use the get_term_link()
function.
<?php foreach (get_terms('actor') as $cat) :
$args = array(
'post_type' => 'movie',
'tax_query' => array(
array(
'taxonomy' => 'actor',
'field' => 'slug',
'terms' => $cat->slug
)
)
);
$movies = get_posts( $args );
$num_movies = count( $movies );
?>
<div class="col-md-4">
<a href="<?php echo get_term_link( $cat->slug, 'actor' ); ?>"><?php echo $cat->name; ?> (<?php echo $num_movies; ?>)</a><br />
</div>
<?php endforeach; ?>