How to Display the Comment Count on Your WordPress Posts and Pages

Home » Snippets » How to Display the Comment Count on Your WordPress Posts and Pages
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Have you ever wanted to display the count of comments on your WordPress blog posts? Adding a comment count can encourage engagement from your readers and help increase the visibility of popular posts. In this article, we’ll show you how to easily display the comment count on your WordPress posts using both code and plugins. Stick around to learn how to implement this feature and make your blog more interactive.

					function wpturbo_comment_count() {
	$num_comments = get_comments_number(); // Get the comment count
	if ( comments_open() ) {
	    if ( $num_comments == 0 ) {
	        $comment_text = __( 'No Comments', 'wpturbo' );
	    } elseif ( $num_comments > 1 ) {
	        $comment_text = $num_comments . __( ' Comments', 'wpturbo' );
	    } else {
	        $comment_text = __( '1 Comment', 'wpturbo' );
	    }
	    $comment_text = '<i class="fa fa-comment-o"></i> ' . $comment_text; // Add your icon before the comment text
	    echo $comment_text;
	} else {
	    $comment_text = __( 'Comments are closed', 'wpturbo' );
	    echo '<i class="fa fa-comment-o"></i> ' . $comment_text; // Add your icon before the comment text
	}
}
				

The following code snippet demonstrates how to display the comment count for any given WordPress post or page. By using this code, you can show the number of comments associated with your content, which can help to encourage discussion and engagement from your readers.

The first part of the code defines a new function called wpturbo_comment_count. This function uses the get_comments_number function to retrieve the total number of comments for the current post or page.

In the next section, we use WordPress control structures to handle the different possible scenarios for the number of comments – if statements are used, so that the code can differentiate between whether there are no comments, there is only one comment or there are multiple comments:

if ( comments_open() ) {
    if ( $num_comments == 0 ) {
        $comment_text = __( 'No Comments', 'wpturbo' );
    } elseif ( $num_comments > 1 ) {
        $comment_text = $num_comments . __( ' Comments', 'wpturbo' );
    } else {
        $comment_text = __( '1 Comment', 'wpturbo' );
    }
}

If comments are open, and there are no comments, the code displays a message of "No Comments". If there is more than one comment, then the comment count is displayed with the number of comments and the message "Comments". If there’s only one comment, the message will be "1 Comment".

Next, we append an icon to the comment count before displaying it in the WordPress post or page:

$comment_text = '<i class="fa fa-comment-o"></i> ' . $comment_text;
echo $comment_text;

In this example, we’re using the Font Awesome "comment" icon to emphasis the comment count. You can use other font icons or your own custom icons by changing the class inside the i element.

Finally, if comments are closed for the post or page, the code displays a message of "Comments are closed":

else {
    $comment_text = __( 'Comments are closed', 'wpturbo' );
    echo '<i class="fa fa-comment-o"></i> ' . $comment_text;
}

This if statement handles the situation when comments are closed. We set the $comment_text variable to "Comments are closed" and add the same icon before displaying it.

Register an account to save your snippets or go Pro to get more features.