How to Customize Comment Count Text in WordPress

Home » Snippets » How to Customize Comment Count Text in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Do you find the default comment count text on your WordPress site boring or not fitting with your website’s style? If so, you’re in luck – customizing the comment count text is easy with just a few lines of code. In this article, we’ll show you how to personalize your comment count text to better suit your website’s theme and tone.

					function wpturbo_custom_comment_count_text( $count ) { 
	$text = _n( 'One Comment', '%s Comments', $count, 'wpturbo' );
	return $text;
}
add_filter( 'get_comments_number_text', 'wpturbo_custom_comment_count_text' );
				

The code snippet above enables you to easily customize the text that displays the number of comments on your WordPress website.

The first line of code defines a new function called wpturbo_custom_comment_count_text(). This function takes the comment count as a parameter and uses WordPress’s _n() function to provide appropriate text depending on the number of comments. The _n() function is a WordPress core translation function that is used to select the singular or plural form of a string based on the number provided as its third parameter.

The second line of code hooks the wpturbo_custom_comment_count_text() function into the get_comments_number_text filter. This filter allows you to modify the text that is output by the get_comments_number_text() function, which is responsible for generating the default comment count text. By hooking your custom function into this filter, you are able to override the number of comments text across your website.

When using this code in your WordPress theme or plugin, make sure to replace the _n() function’s 'One Comment' and '%s Comments' strings with your own text.

For example, if you wanted your comment count text to read "No Comments," "1 Comment," and "%s Comments," you would update the function to look like this:

function wpturbo_custom_comment_count_text( $count ) { 
    $text = _n( 'No Comments', '1 Comment', $count, 'wpturbo' );
    return $text;
}
add_filter( 'get_comments_number_text', 'wpturbo_custom_comment_count_text' ); 

You could also create more elaborate statement for larger number of comments.

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