3

I've created a page on my site to simply list all the subcategories within a category using:

<?php
$args       = array(
                'show_option_all' => '',
                'child_of' => 8,
                'order' => 'DESC',
                'orderby' => 'ID'
);
$categories = get_categories($args);
foreach ($categories as $category) {
                echo '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>' . $category->name . '</a> <br>';
}
?>

I'm trying to include the date of the latest post after the name within parenthesis, but can't seem to figure out a way to do this.

EDIT: Got this working

<?php
// select all sub categories of parent cat with id '8'
$categories = get_categories(['parent' => 8,'orderby' => 'ID','order' => 'DESC',]);
// 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( "F j, Y", 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(); 
    }
?> 
2
<?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();

}

?> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.