Using WordPress ‘comments_number’ PHP filter

The comments_number WordPress PHP filter allows you to modify the comments count for display on your website.

Usage

add_filter('comments_number', 'your_custom_function', 10, 2);

function your_custom_function($output, $number) {
    // your custom code here
    return $output;
}

Parameters

  • $output (string) – A translatable string formatted based on whether the count is equal to 0, 1, or 1+.
  • $number (int) – The number of post comments.

More information

See WordPress Developer Resources: comments_number

Examples

Display “No Comments” instead of “0 Comments”

add_filter('comments_number', 'custom_comments_number_text', 10, 2);

function custom_comments_number_text($output, $number) {
    if ($number == 0) {
        $output = __("No Comments", "text-domain");
    }
    return $output;
}

Add “Awesome” before the comments count

add_filter('comments_number', 'add_awesome_to_comments_count', 10, 2);

function add_awesome_to_comments_count($output, $number) {
    $output = "Awesome " . $output;
    return $output;
}

Display comments count with thousands separator

add_filter('comments_number', 'comments_count_with_separator', 10, 2);

function comments_count_with_separator($output, $number) {
    $output = number_format_i18n($number) . " " . $output;
    return $output;
}

Change “1 Comment” to “1 Remark”

add_filter('comments_number', 'change_single_comment_text', 10, 2);

function change_single_comment_text($output, $number) {
    if ($number == 1) {
        $output = __("1 Remark", "text-domain");
    }
    return $output;
}