Как отобразить post_types из каждой таксономии с комментарием

Я могу показать список таксономии и post_type для каждой таксономии, однако я хочу показать также 1 комментарий для каждого post_type, но безуспешно.

<?php 

$custom_terms = get_terms('article-category');

foreach($custom_terms as $custom_term) {
wp_reset_query();
$args = array(
    'parent'=>0,
    'post_type' => 'article',
    'tax_query' => array(
        array(
            'taxonomy' => 'article-category',
            'field' => 'slug',
            'terms' => $custom_term->slug,
        ),
    ),
 );

 $loop = new WP_Query($args);
 $comments = get_comments($args); // trying to get 1 comment for each post_type
 if($loop->have_posts()) {
    echo '<h2>'.$custom_term->name.'</h2>';

    while($loop->have_posts()) : $loop->the_post();
        echo '<a href="'.get_permalink().'">'.get_the_title().'</a><br>';
        echo($comment->comment_content); // but not showing
    endwhile;
 }
}

?>

Ниже приведен пример, который работает: он показывает каждую таксономию как категорию и post_type как статью 1, 2, 3, но я не могу показать 1 комментарий от каждой статьи post_type. Каждая статья имеет только 1 комментарий.

Category 1
 article 1 title
   1 recent comment from article 1  // this is not displaying

 article 2 title
   1 recent comment from article 2 // this is not displaying

 article 3 title
   1 recent comment from article 3 // this is not displaying

Category 2
 article 1 title
   1 recent comment from article 1 // this is not displaying

 article 2 title
   1 recent comment from article 2 // this is not displaying

 article 3 title
   1 recent comment from article 3 // this is not displaying

1 ответ

Вам нужно установить 'post_id' в $args, чтобы получить комментарии. Массив объектов возвращается, если комментарии существуют, затем используйте функцию сброса, чтобы получить первый комментарий.

$args = array( 'post_id' => 123 );
$comments = get_comments( $args );

if( $comments ) {
    $first_comment = reset($comments);
    echo $first_comment->comment_content
} 
Другие вопросы по тегам