Comments are a great feature to engage the visitors on your website. If you can’t build a separate forum for engaging your visitors and for building a community, the comments are the best way to provide an open community to the visitors. They can ask questions to each other and share ideas there and do many things to build a good community on your site. But, the default WordPress comments provided under some specific posts or on the sidebars are not yet capable of producing such a huge impact on building a live community.
Reasons to display recent comments on WordPress
- To show users what others are talking about on your website.
- If you are running a community based on WordPress.
- Encourage other users to get part of the community on your site.
- For administrative purposes like displaying the user comments on an admin-only page.
Methods to display recent comments
How would it be if you build a separate comments page for your readers? There the visitors can find out the recent comments, active users, etc… and answer to the threads.
Method 1. Using code
If you do not want to use any plugin to display the recent comments on your WordPress website, you can use the following code snippet to create a shortcode. Place it in your theme’s functions.php file. Once placed, you can use the shortcode [recent-comments] anywhere in a page to display the comments.
function display_recent_comments_shortcode( $atts ) {
$args = shortcode_atts( array(
'number' => 5, // number of comments to display
'status' => 'approve', // only show approved comments
'post_status' => 'publish' // only show comments on published posts
), $atts );
$comments = get_comments( $args );
ob_start(); ?>
<ul>
<?php foreach ( $comments as $comment ) : ?>
<li><?php echo $comment->comment_author . ': ' . $comment->comment_content; ?></li>
<?php endforeach; ?>
</ul>
<?php
$output = ob_get_clean();
return $output;
}
add_shortcode( 'recent-comments', 'display_recent_comments_shortcode' );
You can also use the attributes in the shortcode like this –
[recent-comments number="10" post_status="publish"]
Method 2. Using Decent Comments plugin
A plugin namely Decent Comments will help you do so. Just download and install it on your WordPress website. If you are unsure about installing the plugin. Read our step-by-step guide to install a plugin on a WordPress website.
For displaying your comments, you need a separate page. So, create one. After creating the page, just paste this shortcode code to the content area.
[decent-comments/]
Now, preview the page and you will see 5 of the most recent comments alongside the user avatars. If you wish to increase the comments count or change the avatar size, use the code below.
[decent-comments number="25" avatar_size="32" /]
In this example, I used 25 comments and 32 pixels size for the avatars. Replace the number and size with what you want. If you want to explore more shortcodes, visit the documentation page of the plugin.
Now, the recent comments page might look something like the above screenshot. Congratulations! You got a good recent comments page. Thus, your visitors will be some more engaged in your website. Display the recent comments page, some good places like menu bar or sidebars to drive them to the page.
You can also take a look at other WordPress comment plugins and see if they provide similar functionality.
Conclusion
We hope this article helped you to create a recent comments page. Recent comments can be a great way to see what your users are talking about on your entire WordPress website. You can engage with them and build a healthy, active community on your WordPress website.