How to display the most recent comments in WordPress

Even if you don’t like to leave comments, once you get your WordPress website up and running, you will want people to write a word of appreciation below your articles. The more people start to comment on your articles, the more popular your website gets and it becomes important that you take care of comments.

There’s so much you can do with comments. In the following days, we’re about to show you how to allow your visitors to mark their comments with some extra HTML tags and how to forbid HTML tags to be used at all. You will learn how to limit the number of characters in their replies or even enable usage of shortcodes in comments.

In addition to all that, in this article, we’re about to show you how to list the most recent comments in WordPress and how to do that without using plugins.

If you’ve got acquainted with WordPress, you’ve probably found out about its Recent Comments widget which allows you to easily show any number of last comments on your site. The widget can help you attract visitors into commenting by showing them what other people had to say about different topics on your site. Even though it is very simple, if you want to show the most recent comments on a sidebar, this widget will be more than enough.

But if you wanted more control over the most recent comments, you will need a different approach. With the power of this function, you will be able to insert the list of the most recent comment anywhere on your site and be able to easily style the list. Ready to create your own list?

Display the most recent comments in WordPress:

  1. Open template file where you want your comment list to appear (sidebar.php, footer.php, single.php…)
  2. Copy and paste the following code:
  3. $args = array (
    'status' => 'approve',
    'number' => '5'
    $comments = get_comments( $args );
    if ( !empty( $comments ) ) :
    echo '<ul>';
    foreach( $comments as $comment ) :
    echo '<li><a href="' . get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID . '">' . $comment->comment_author . ' on ' . get_the_title( $comment->comment_post_ID ) . '</a></li>';
    echo '</ul>';
  4. Change the number of comments which will be displayed on the 3rd line
  5. Save changes

If you haven’t done any changes to this code, it will show last 5 comments created on your site. The function will only show approved comments and will show author’s name and the post it was created on.

Recent comments in WordPress

Style the list:

If you want to style the list, you can easily add a CSS class to it and modify the list. In the following example, let’s modify the code and a class to our list which will change its color to red:

  1. Modify the code above and change the 9th line to this:
  2. echo '<li class="recentcomments"><a href="' . get_permalink( $comment->comment_post_ID ) . '#comment-' . $comment->comment_ID . '">' . $comment->comment_author . ' on ' . get_the_title( $comment->comment_post_ID ) . '</a></li>';
  3. Save changes
  4. Open style.css files
  5. Copy and paste the following:
  6. .recentcomments {
    color: red;
  7. Save changes

Recent comments red list

As you can see, the CSS in this example is as simple as it gets. You can, of course, style the list, author and comment links to whatever you want to. Take your time and try out different styles on your newly created list before you publish it online.

We hope that you liked the function. If so, don’t forget to check out the rest of our WordPress tutorials which can help you create a unique WordPress website.

Start Blog Book


This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

One thought on “How to display the most recent comments in WordPress

  1. I am trying to customize the Recent Comments, either by widget or by modifying the theme (as in your article).

    I have restricted certain posts to particular user roles. What I’d like is, when a user has access to a particular post, the recent-comments list is limited to just those posts that the user has access to. For example, Joe has a paid registration to take the online course “Farm Animals.” His user role is “farm” which allows him to view the posts that have that course’s category attached to them. If he goes to one of those posts, I want him to see a list of recent comments on all the posts associated with that course, but I DON’T want him to see a list of comments from some other course that he didn’t enroll in.

    Any ideas how to do this? I imagine it involves using a PHP conditional to check the category of the current post, look for recent comments in just that category, and display a list of links to those comments.

    By the way, the reason for this is that I want to protect users from their screen name being viewed by users in other courses.

    I’ve been researching this for hours and getting nowhere. Any help is greatly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *