Recent comment in WordPress helps you to show that your site has dynamic content. In order to keep users engaging on your site, you can show recent comments, either sidebar or in the home page or footer of your site. This article will share new different methods, you can use to show recent comments on your website.
Show recent comments in WordPress with or without plugin
Show recent comments in WordPress using plugin
NOTE: This works in latest WordPress version. Tested with Version 6.2.2.
The easiest solution is to use Recent Comments plugin by creators of WordPress, i.e. Automattic.
This plugin creates functions to assist in displaying a list of the most recent comments. Provides more configurability than the widget that comes with WordPress.
You may pass parameters when calling the function to configure some of the options. Parameters are accepted only in the query-string-style.
This plugin creates list_most_recent_comments()
function.
In addition to the parameters that get_comments()
and get_most_recent_comments()
(see below) accept, this function accepts the following parameters:
- excerpt_words — The number of words from the comment to display
- excerpt_chars — Or alternately the number of characters from the comment to display
- comment_format — Allows you to pick from two predefined display formats:
- Comment Author on Post Title
- Comment Author: This is the comment excerpt.
Show recent comments in WordPress without plugin
NOTE: This code works up to WordPress Version 5.4.2.
You can add this manual PHP code to display recent comments anywhere on your WordPress site.
Simply add this code to PHP file, such as sidebar.php to show recent comments.
<?php function recent_comments( $posts = 5, $size = 70 ) { global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_author_email, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,70) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT ".$posts; $comments = $wpdb->get_results($sql); foreach ($comments as $comment) { ?><ul><li><p><strong><a href="<?php echo get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID; ?>" title="Read the comment and explore article" rel="bookmark"><?php echo strip_tags($comment->comment_author); ?></a></strong> : <?php echo strip_tags($comment->com_excerpt); ?>... <time> <?php echo human_time_diff(get_comment_date('U',$comment->comment_ID), current_time('timestamp')), __(' ago', 'technoyl'); ?></time></p></li></ul> <?php } } ?> <?php recent_comments('5', '70'); ?>
That’s it!