A
A
Andrey2018-09-22 15:55:32
WordPress
Andrey, 2018-09-22 15:55:32

List of recent comments from a specific category?

There is a code for displaying the latest comments. Please tell me how to set the output of the last comments only from a specific category in this code. And how to display the total number of comments in the post near the title of the post?

$comment_len = 140;
    $comments = get_comments('status=approve&number=15');
    if ($comments) {
        foreach ($comments as $comment) {
            ?>
            <li>
               <?php echo get_avatar($comment,$size='50' ); ?>
                <?php echo $comment->comment_author; ?>
        <?php
                  $d = "j F Y в H:i";
                  $comment_ID = $comment->comment_ID;
                  $comment_date = get_comment_date( $d, $comment_ID );
                  echo $comment_date;
                  ?>
        
               <a href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>"><?php echo mb_substr( strip_tags( $comment->comment_content ), 0, 75 ); ?>...</a>
        <a href="<?php echo get_permalink($comment->comment_post_ID); ?>"><?php echo get_the_title($comment->comment_post_ID); ?></a>
            </li>
            <?php
        }
    } else {
        echo "<li>Комментарии отсутствуют</li>";
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Sobolev, 2018-09-22
@san_jorich

Perhaps not the most juicy solution, but you do not have enough selection of posts of the necessary headings.
Add something like:

<?php 
$needed_ids = array(); 
$posts = get_posts( array('category'    => /* id или slug-и нужных рубрик*/) );
foreach( $posts as $post ){ array_push($needed_ids, $post->ID); } 
?>

and, later, in your code, insert the argument'post__in' => $needed_ids

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question