我正在尝试更改基于 WooCommerce 的 WP 网站上的一些文本。

本质上,通过使用 str_replace 的过滤器,我试图将文本“%s 对 %s 的评论”更改为其他内容。

此文本包含在 single-product-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,但在这种情况下似乎不起作用。

操作更新

我尝试过使用 gettext,但在这种情况下似乎不起作用。

如前所述,我的目标是以下代码(在 single-product-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”来替换“评论”,它就可以正常工作。如果我尝试用它来替换“% review for %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> 


评论关闭
IT干货网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!