You will need to combine get_categories()
and wp_query
First you should get all categories and loop through each one of them. Inside the loop you need a wp_query
to get the title and the image of the latest post per category. At last you should reset the post data.
Place the code below inside your homepage template:
$args = array (
'hide_empty' => 1,
);
$terms = get_categories( $args );
foreach ( $terms as $term ) {
$query = new WP_Query( array( 'post_type' => 'post', 'cat' => $term->term_id, 'posts_per_page' => 1 ) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$postlink = get_the_permalink( $query->ID );
$postimage = wp_get_attachment_url( get_post_thumbnail_id( $query->ID ) );
echo '<h4><img src="'.$postimage.'"><a href="'.$postlink.'">'.$term->name.'</a></h4>';
}
}
// Restore original Post Data
wp_reset_postdata();
}