关于WordPress中的评论.php模板和标题的问题



我正在 developer.wordpress.org 上的"主题手册"的帮助下学习构建主题,评论模板中有几行我不清楚。

这里的每一段代码到底发生了什么?

<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>

我知道它自定义了评论部分的标题,并且_nx用于翻译目的,但是详细发生了什么?

谢谢

如果您在理解像这样的表达式时遇到困难,一个好的做法是将其分解为多个语句。

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>'
);

否则写:

$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);

_nx()的文档说,它应该根据$commentNumber变量返回输入的$singularForm或$pluralForm版本。

由于它与 printf 结合使用,我假设它返回的内容带有两个替换字符,这些字符将变得$commentNumber$htmlTitle

如果您有任何疑问,您可以随时在某处将这个解压缩的脚本与echo $nx;语句一起使用。

最新更新