taxonomy-actor

I have a piece of code that lists all the actors I have in my db.
[php]<?php foreach (get_terms('actor') as $cat) : ?>

<div class="col-md-4">

	<a href="/wordpress/actor/<?php echo $cat->name; ?>"><?php echo $cat->name; ?></a><br />

</div>
<?php endforeach; ?>

[/php]

#1 how do I add the amount of custom post type movies at associated with each actor.

#2 hyperlink it to taxonomy-actor.php

tried a few things but keep getting 404 page.

#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; ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service