Michel
Michel

Reputation: 41

question about the comment.php template and title in wordpress

I'm learning to build themes with the help of the "theme handbook" on developer.wordpress.org and there's a few lines in the comment template that are not clear to me.

what is exactly happening in every bit of code here?

<h2 class="comments-title">
        <?php
            printf( _nx( 'One thought on "%2$s"', '%1$s thoughts on "%2$s"', get_comments_number(), 'comments title', 'twentythirteen' ),
                number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' );
        ?>
    </h2>

I understand that it customizes the title of the comment section and that _nx is for translation purposes, but what is happening in detail?

thank you

Upvotes: 1

Views: 114

Answers (1)

y_nk
y_nk

Reputation: 2275

If you have trouble understanding an expression which is packed such as this one, a good practice is to decompose it into multiple statements.

printf(
  _nx(
    'One thought on "%2$s"',
    '%1$s thoughts on "%2$s"',
    get_comments_number(),
    'comments title',
    'twentythirteen'
  ),
  number_format_i18n(
    get_comments_number()
  ),
  '<span>'.get_the_title().'</span>'
);

otherwise written:

$singularForm = 'One thought on "%2$s"';
$pluralForm = '%1$s thoughts on "%2$s"';

$commentNumber = get_comments_number();
$i18nNumber = number_format_i18n($commentNumber);

$htmlTitle = '<span>'.get_the_title().'</span>';

$nx = _nx($singularForm, $pluralForm, $commentNumber, 'comments title', 'twentythirteen');
printf($nx, $i18nNumber, $htmlTitle);

The documentation of _nx() says that it should return either the $singularForm or $pluralForm version of your input depending on the $commentNumber variable.

As it is combined with printf, I assume that it returns rather something with two replacement characters, which will become $commentNumber and $htmlTitle.

If you have a doubt, you can always use this unpacked script with an echo $nx; statement somewhere.

Upvotes: 1

Related Questions