使用str_replace更改PHP模板中的文本
我正在尝试更改我基于WooCommerce的WP网站上的一些文本 实际上,通过使用str_replace的筛选器,我正在尝试将文本“%s reviews for%s”更改为其他内容 此文本包含在single-product-reviews.php文件中 见下文:使用str_replace更改PHP模板中的文本,php,wordpress,woocommerce,str-replace,Php,Wordpress,Woocommerce,Str Replace,我正在尝试更改我基于WooCommerce的WP网站上的一些文本 实际上,通过使用str_replace的筛选器,我正在尝试将文本“%s reviews for%s”更改为其他内容 此文本包含在single-product-reviews.php文件中 见下文: <h2><?php if ( get_option( 'woocommerce_enable_review_rating' ) === 'yes' && ($count = $product->g
<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,但在这种情况下似乎也不起作用
操作更新
我尝试过使用gettext,但在这种情况下它似乎不起作用
如前所述,我的目标是以下代码(在singleproductreview.php中)
如果我用“gettext”来代替“Reviews”,效果很好。如果我试图让它替换%s的“%review”,它将不起作用
你知道为什么吗 您可以通过两种方式完成此操作: 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>
到
通过将模板覆盖复制到主题的
woocommerce
文件夹中,您可以使用模板覆盖来覆盖single product reviews.php
或者您可以从主题的functions.php
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;
}
嗨@helgatheviking,Pieter Goosen,谢谢你的帮助。这个“gettext”过滤器在某些情况下似乎工作得很好,但并非适用于所有情况。例如,我成功地使用它将文本“相关产品”更改为“相关机会”。输出“相关产品”的代码位于“Related.php”文件“code”中,如果($Products->have_posts()):?>“code”,但是,出于某种原因,该gettext似乎无法更改single-product-review.php文件中的代码。有什么想法吗?也许
是单数的(
条件?也许你有其他的插件?您应该能够在switch语句中添加“相关产品”作为另一个案例。谢谢@Mr Jerry。请问在哪个语言文件中可以找到?干杯,Linzyou可以添加wp内容/语言
或wp内容/主题/您的当前主题/语言
<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>
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;
}