[Support request] How to change ‘X’ thoughts on ‘post title’ in comment section

Home Forums Support [Support request] How to change ‘X’ thoughts on ‘post title’ in comment section

Home Forums Support How to change ‘X’ thoughts on ‘post title’ in comment section

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1373134
    Shami

    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?

    #1373187
    David
    Staff
    Customer Support

    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()
        );
    } );
    #1373368
    Shami

    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?

    #1373373
    David
    Staff
    Customer Support

    Yep thats correct.

    #1591610
    Jesse

    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!

    #1591636
    Elvin
    Staff
    Customer Support

    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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.