Archived topic
How to change ‘X’ thoughts on ‘post title’ in comment section
5 replies · Started by Shami on July 23, 2020
I just wish If I could change the title of the comment section.
Instead of: ‘X’ thoughts on ‘post title’
I want to make it: Reader Comments (count)
Is there any easy fix for it?
Hi there,
try this PHP Snippet:
add_filter( 'generate_comment_form_title', function() {
$comments_number = get_comments_number();
return sprintf( // WPCS: XSS OK.
/* translators: 1: number of comments */
esc_html( _nx(
'Reader Comments %1$s',
'Reader Comments %1$s',
$comments_number,
'comments title',
'generatepress'
) ),
number_format_i18n( $comments_number ),
get_the_title()
);
} );
Thanks, David. It worked perfectly.
There is one thing though: The above code doesn't put brackets around the comment's count. So I placed them myself.
I replaced this:
/* translators: 1: number of comments */
esc_html( _nx(
'Reader Comments %1$s',
'Reader Comments %1$s',
$comments_number,
'comments title',
'generatepress'
) ),
With this:
/* translators: 1: number of comments */
esc_html( _nx(
'Reader Comments (%1$s)',
'Reader Comments (%1$s)',
$comments_number,
'comments title',
'generatepress'
) ),
It works fine now. Did I do it right?
Yep thats correct.
Hi David,
Is there a way to improve upon this snippet so it will show “1 comment” instead of “1 comments” if there is only one comment?
Thanks!
Hi Jesse,
You'll have to set the values inside _nx() checking the comment count:
Something like this should work.
add_filter( 'generate_comment_form_title', function(){
$comments_number = get_comments_number();
return sprintf(
esc_html(
/* translators: 1: number of comments, 2: post title */
_nx(
'%1$s Comment on “%2$s”',
'%1$s Comments on “%2$s”',
$comments_number,
'comments title',
'generatepress'
)
),
number_format_i18n( $comments_number ),
get_the_title()
);
});
Simply remove on “%2$s” if you don't want the title.