web-dev-qa-db-ja.com

カテゴリ一覧の最新の投稿からの最終更新日を表示する方法

私は自分のサイトに、カテゴリ内のすべてのサブカテゴリをリストするためのページを作成しました。

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

私は括弧内に名前の後に最新の投稿の日付を含めようとしていますが、これを行う方法を考え出すことができないようです。

編集:この作業を得た

<?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