wp_count_comments

Home » Functions » wp_count_comments

WordPress is a robust content management system that comes with many built-in functions to make it easy to manage and customize your website. One such function is wp_count_comments, which is used to count the number of comments on a post or page.

The wp_count_comments function takes a single argument, which is the ID of the post or page that you want to count the comments for. The function then returns an array with the number of comments and the number of approved comments for that post or page.

This function is particularly useful for displaying the number of comments on a post or page, which can be a great way to encourage engagement and discussion on your website. You can use the returned values to display the comment count in a variety of ways, such as in a sidebar widget or in the post meta information.

Here’s an example usage code:

$comments_count = wp_count_comments( get_the_ID() );
echo 'This post has ' . $comments_count->approved . ' approved comments and ' . $comments_count->total_comments . ' total comments.';

In this example, we’re using the wp_count_comments function to get the number of approved and total comments for the current post using the get_the_ID function. We’re then using the returned values to display the comment count in a sentence.

Learn More on WordPress.org

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