The get_comments_number_text() WordPress PHP function displays the language string for the number of comments the current post has.
Usage
Here’s a simple way to use the get_comments_number_text() function:
$comments_text = get_comments_number_text('No comments', 'One comment', '% comments', $post_id); echo $comments_text;
This will output a string depending on the number of comments a post has. If there are no comments, it will output “No comments”, if there’s one comment, it outputs “One comment”, and if there are more than one comments, it will output the number of comments with a “%” sign replacing the actual number of comments.
Parameters
- $zero (string – Optional): Text for no comments. Default: false.
- $one (string – Optional): Text for one comment. Default: false.
- $more (string – Optional): Text for more than one comment. Default: false.
- $post (int|WP_Post – Optional): Post ID or WP_Post object. Default is the global $post.
More information
See WordPress Developer Resources: get_comments_number_text()
Note: % symbol is supported in the $more argument and can be used to position the comment count. However, % symbol is not supported in the $one label – you have to add the comment count to the label yourself.
Examples
Show number of comments on a blog post
In this example, we’re going to show the number of comments on a specific blog post.
$post_id = 123; // Replace with your post ID $comments_text = get_comments_number_text('Be the first to comment', 'One person has commented', '% people have commented', $post_id); echo $comments_text;
Custom text for no comments
In this example, we customize the message when there are no comments on a post.
$post_id = 123; // Replace with your post ID $comments_text = get_comments_number_text('No one has spoken up yet', 'One person has spoken up', '% people have spoken up', $post_id); echo $comments_text;
Display comments count in a different language
In this example, we display the comments count in Spanish.
$post_id = 123; // Replace with your post ID $comments_text = get_comments_number_text('No hay comentarios', 'Un comentario', '% comentarios', $post_id); echo $comments_text;
Use within the WordPress loop
In this example, we use the function within the WordPress loop without specifying a post ID.
if (have_posts()) { while (have_posts()) { the_post(); $comments_text = get_comments_number_text('No comments', 'One comment', '% comments'); echo $comments_text; } }
Display comments number with custom positioning
In this example, we show how to use the ‘%’ symbol for custom positioning of the comments count.
$post_id = 123; // Replace with your post ID $comments_text = get_comments_number_text('No comments', 'One comment found', 'Found % comments', $post_id); echo $comments_text;