使用 str_replace 更改 PHP 模板中的文本
Using str_replace to change text within PHP template
我正在尝试更改基于 WooCommerce 的 WP 网站上的一些文本。
基本上,通过使用 str_replace 的过滤器,我试图将文本“%s 对 %s 的评论”更改为其他内容。
此文本包含在单品-reviews.php 文件中。
见下文:
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
为了做到这一点,我尝试使用以下方法,但它似乎不起作用。我不确定我应该定位哪个字符串。
function lnz_replace_content()
{
echo str_replace("%s reviews for %s","%s comments about %s", $product);
}
add_filter('init','lnz_replace_content');'
我也尝试过 gettext,但在这种情况下似乎也不起作用。
OP更新
我试过使用 gettext,但在这种情况下似乎不起作用。
如前所述,我针对以下代码(在单一产品中-review.php)
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ( $count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
如果我使用 'gettext' 替换 'Reviews',它工作正常。如果我尝试用它来替换“%s 的 % 评论”,它是行不通的。
知道为什么。
您可以通过两种方式完成此操作:
1) 语言文件更改:
msgid "%s reviews for %s"
msgstr "%s comments about %s"
msgid "%s review for %s"
msgstr "%s comment about %s"
2) 更改代码:
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
到
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s comment about %s', '%s comment about %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
您可以使用模板覆盖来覆盖 single-product-reviews.php
,方法是将其复制到主题的 woocommerce
文件夹中。
或者您可以从主题 functions.php
中过滤 gettext
add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
/**
* Change comment form default field names.
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
function theme_change_comment_field_names( $translated_text, $text, $domain ) {
if ( is_singular() ) {
switch ( $translated_text ) {
case '%s reviews for %s' :
$translated_text = __( '%s comments for %s', 'theme_text_domain' );
break;
case 'Related Products' :
$translated_text = __( 'Related Opportunities', 'theme_text_domain' );
break;
}
}
return $translated_text;
}
我正在尝试更改基于 WooCommerce 的 WP 网站上的一些文本。
基本上,通过使用 str_replace 的过滤器,我试图将文本“%s 对 %s 的评论”更改为其他内容。
此文本包含在单品-reviews.php 文件中。
见下文:
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
为了做到这一点,我尝试使用以下方法,但它似乎不起作用。我不确定我应该定位哪个字符串。
function lnz_replace_content()
{
echo str_replace("%s reviews for %s","%s comments about %s", $product);
}
add_filter('init','lnz_replace_content');'
我也尝试过 gettext,但在这种情况下似乎也不起作用。
OP更新
我试过使用 gettext,但在这种情况下似乎不起作用。
如前所述,我针对以下代码(在单一产品中-review.php)
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ( $count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
如果我使用 'gettext' 替换 'Reviews',它工作正常。如果我尝试用它来替换“%s 的 % 评论”,它是行不通的。
知道为什么。
您可以通过两种方式完成此操作:
1) 语言文件更改:
msgid "%s reviews for %s"
msgstr "%s comments about %s"
msgid "%s review for %s"
msgstr "%s comment about %s"
2) 更改代码:
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s review for %s', '%s reviews for %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
到
<h2><?php
if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->get_review_count() ) )
printf( _n( '%s comment about %s', '%s comment about %s', $count, 'woocommerce' ), $count, get_the_title() );
else
_e( 'Reviews', 'woocommerce' );
?></h2>
您可以使用模板覆盖来覆盖 single-product-reviews.php
,方法是将其复制到主题的 woocommerce
文件夹中。
或者您可以从主题 functions.php
gettext
add_filter( 'gettext', 'theme_change_comment_field_names', 20, 3 );
/**
* Change comment form default field names.
*
* @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
*/
function theme_change_comment_field_names( $translated_text, $text, $domain ) {
if ( is_singular() ) {
switch ( $translated_text ) {
case '%s reviews for %s' :
$translated_text = __( '%s comments for %s', 'theme_text_domain' );
break;
case 'Related Products' :
$translated_text = __( 'Related Opportunities', 'theme_text_domain' );
break;
}
}
return $translated_text;
}