How to display Last Updated date from the latest post for a category list

<?php

// select all sub categories of parent cat with id '8'
$categories = get_terms( 'category', array(
                                   'orderby' => 'ID',
                                   'parent'  => 8
                                    ) 
                                 );

// For each sub category find last post
foreach ( $categories as $category ) {

    $args = array(
    'cat'            => $category->term_id,
    'post_type'      => 'post',
    'posts_per_page' => '1',
    'orderby'        => 'date',
    'order'          => 'DESC'
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) { 

       while ( $query->have_posts() ) {

            $query->the_post();

            echo '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name . ' ('. get_the_date( ** FORMAT HERE **, get_the_id() ) .')</a> <br>'; 

       }

    } else {

         echo '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name . '</a> <br>'; 

    }

    wp_reset_postdata();

}

?> 

Leave a Comment