This could be done with a ne walker class
class Childcat2LastPost extends Walker_Category
{
function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
if ( 0 != $category->category_parent ) {
$args = array(
'cat' => $category->term_id,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'post__not_in' => get_option( 'sticky_posts' ), // do not display sticky posts
);
$query = new WP_Query( $args );
$recent_posts = $query->get_posts();
$last = sizeof( $recent_posts ) -1;
$last_post = $recent_posts[$last];
$permlink = get_permalink( $last_post->ID );
$title = $last_post->post_title;
if ( ! empty( $permlink ) ) {
$output .= sprintf( '<li><a href="https://wordpress.stackexchange.com/questions/97531/%s">%s</a></li>', esc_attr( $permlink ), esc_html( $title ) );
}
} else {
parent::start_el( $output, $category, $depth, $args, $id );
}
}
}
You have to pass the class as argument to wp_list_categories
$cat2post = new Childcat2LastPost();
$args = array(
'walker' => $cat2post
);
echo '<ol>';
wp_list_categories( $args );
echo '</ol>';
Comment out the line 'post__not_in'
if you want to include sticky posts. Adjust the HTML in the line with $output .= sprintf( '<li><a href="https://wordpress.stackexchange.com/questions/97531/%s">%s</a></li>', esc_attr( $permlink ), esc_html( $title ) );
(add an alt
and/or title
attribute, a class etc).