In this post, we will see how to get a number of comments in WordPress.You will be able to count comments of a post in WordPress.WordPress Provides a very fantastic commenting system that allows readers to add comments to the blog.
Comment section plays vital role to the web blog and its a kind of review of visitors so equally important to post.Almost Every blog allow visitors to post comments about the content. This gives readers the opportunity to interact with the writer of the blog and also for making the whole enterprise interactive. With the visitors comment, you can collect reviews of your content.
Today I am going to share function which will provide a count of comments of a particular post in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | // Get number of comments of a post function get_comments_count( $post_id, $nocomments , $onecomment, $morecomments ) { $comments_count = get_comments_number( $post_id ); if ( $comments_count == 0 || empty( $comments_count ) ) { return $nocomments; } elseif ( $comments_count == 1 ) { return '1 ' . $onecomment; } elseif ( $comments_count > 1 ) { return $comments_count . ' ' . $morecomments; } else { return ''; } } |
Explanation:
Here in this function, you need to pass post_id, the sentence for no comment,1 comment and more comments which will check and return the count of the comments.You can add this function to the functions.php file of active theme or where you want to use.
Hope you find this function useful for you. will wait for your responses.
Comments (1)