How to display total comments in WordPress – Learn WordPress from a to z

Tutorials 0 lượt xem

Comments allow users to engage and interact with the content on your site. By showing the total number of comments, you can encourage more users to join the conversation.

In this article, I will show you how to easily display the total number of comments in WordPress in two ways, with or without a plugin.

show total number of comments in wordpress

Show total comments in WordPress using a plugin

This method is easier and it is recommended for all users. Instead of writing code, you will use a plugin to display the number of comments in WordPress. First, you need to install and activate the Simple Blog Stats plugin .

Upon activation, go to Settings »  Simple Blog Stats to configure settings for the plugin.

plugin simple blog stats

On this page you will see all the shortcodes that you can use to display various statistics like total number of comments, registered users, number of posts, categories, number of pages, etc.

You need to copy the shortcode [sbs_approved] to display the total number of approved comments on your WordPress site.

install plugin simple blog stats

If you want to display it in a post or page, then just create a new post or edit an existing one.

On the post edit screen, you need to add the shortcode in the WordPress content editor.

add shortcode to the page

You can do this by clicking on the Add block icon (+) and then searching for the shortcode. Once you find it, you need to click on it to add the shortcode to the editor.

Next, you need to paste the shortcode [sbs_approved] inside the block settings.

paste shortcode

You can add any text you want to display before or after the shortcode.

Use shortcode inside paragraph

Alternatively, you can also use shortcodes inside paragraphs. Just copy and paste the shortcode where you want the comment count to be displayed.

announcements

Using a paragraph block will also allow you to use the text styling options available in WordPress’ block editor.

Once done, click the publish or update button to save your changes. You can now visit your website to see the changes.

preview shows total comments

Show total comments in sidebar widget

You can also use the same shortcode inside a WordPress widget and display it in the sidebar of your blog.

Just go to Appearance »  Widgets page and add the Text widget to the sidebar.

add widgets

Next, go ahead and paste your shortcode inside the Text widget. Don’t forget to click the Save button to save your widget settings. You can now visit your website to see the widget in action.

show sidebar comments

Show total number of WordPress comments (no plugin needed)

If you’re not using a plugin, you can manually display the total number of comments. This method requires you to add the code snippet to your website.

Before you do, you should back up your entire website. This will help you recover your site in the unlikely event that something goes wrong.

Now, you need to add the following code to your theme or child theme’s functions.php file:

function wpb_comment_count() { 
    $comments_count = wp_count_comments();
    $message =  'There are '<strong>'.  $comments_count-&gt;approved . ''</strong> comments posted by our users.';
 
    return $message; 
} 

add_shortcode('wpb_total_comments','wpb_comment_count'); 
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span>
</div>

This code creates a function that outputs the total number of approved WordPress comments. It also generates a shortcode to display it.

You can now use the shortcode [wpb_total_comments] in your posts, pages or text widgets to display the total number of comments on your site. Please see the display after completion.

total comments

Epilogue

Hopefully the above 2 ways will help you easily display the total number of comments in WordPress.

If you find it interesting, you can follow the  WordPress basics section  to know more new knowledge.

Follow fanpage to receive the latest posts:  Group

Bài viết liên quan