-1

I want to show 10 recent comments along with post titles on WordPress blog homepage. For this purpose, I am searching since last two days but I am unable to find any solution, Can somebody tell me how to do this?

0

There's this built-in function for querying comments get_comments and here's something that would help in your template:

$comments = get_comments( array( 'number' => 10 ) );

if ( !empty( $comments ) ) {

    ?>

    <ul>

    <?php foreach ( $comments as $comment ) : ?>

        <li><?php echo sprintf(

            "<strong>%s</strong> on <a href=\"%s\">%s</a> : \"%s\" &mdash; <a href=\"%s\">view</a>",
            $comment->comment_author,
            get_the_permalink( $comment->comment_post_ID ) . "#comment-" . $comment->comment_ID,
            get_the_title( $comment->comment_post_ID ),
            mb_strlen( $comment->comment_content ) > 100 ? mb_substr($comment->comment_content, 0, 100) . " .." : $comment->comment_content,
            get_the_permalink( $comment->comment_post_ID ) . "#comment-" . $comment->comment_ID

        ); ?></li>

    <?php endforeach; ?>

    </ul>

    <?php

} else {
    echo sprintf("<p>%s</p>", "No comments were found.");
}

If you want a short-code or a custom function and needed help, comment below.

Hope that helps.

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.