The reason you want to count the comments of a post is obvious. After all, comments are the primary way we communicate with our website/blog visitors.
We’ll review the
get_comments_number function and try its usage in a working example.
get_comments_number function syntax
WP developers can use
get_comments_number function to get the number of comments on a post.
This function accepts one argument for post reference.
$post_id can be a specific post’s ID or a post object.
Counting comments in WordPress using
The easiest way to retrieve comments number is to use
get_comments_number function in WP.
I’m going to show you, how you can retrieve the number of comments on a post with ID 1.
Working example of
$post_id = 1; //set the post id get_comments_number($post_id);