web-dev-qa-db-fra.com

Comment afficher la date de la dernière mise à jour du dernier message pour une liste de catégories

J'ai créé une page sur mon site pour répertorier simplement toutes les sous-catégories d'une catégorie à l'aide de:

<?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>';
}
?>

J'essaie d'inclure la date du dernier message après le nom entre parenthèses, mais je n'arrive pas à trouver un moyen de le faire.

EDIT: ça marche

<?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(); 
    }
?> 
3
rizarjay
<?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();

}

?> 
2
lukgoh