Get image of latest post from taxonomies/categories

Question

I have a custom post type “Artworks” with a custom taxonomy “Artists”. I have a page where I list all the entries from the “Artists” taxonomy (Artist 1, Artist 2, Artist 3, etc.).

How can I get the picture from the latest post in each category (Artist 1, Artist 2, etc.)?

That’s how I list the entries from the “Artists” taxonomy:

<style>
    .artistbox {
        width:100%;
        height:150px;
    }
</style>

<?php
    $tax_terms = get_terms('artists', array('hide_empty' => '0'));      
        foreach ( $tax_terms as $tax_term ):  
            echo '<div class="artistbox"  style="background-image: url('.$latestimage.');"><a href="/artists/'.$tax_term->slug.'">'.$tax_term->name.'</a></div>';  
        endforeach;
?>

I want $latestimage to be the picture of the latest post from each category. How can I do that?

Like this:

Artist 1: Featured img of latest post of cat Artist 1

Artist 2: Featured img of latest post of cat Artist 2

Artist 3: Featured img of latest post of cat Artist 3

0
, , , , HAL2020 3 years 2020-04-07T08:51:55-05:00 0 Answers 86 views 0

Leave an answer

Browse
Browse