Joe*_*ggs 2 php wordpress templates gettext woocommerce
我以前使用以下功能来更改Woocommerce中的相关产品文本.
function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Related Products' :
$translated_text = __( 'Related Books', 'woocommerce' );
break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );
Run Code Online (Sandbox Code Playgroud)
它总是很完美,但从Woocommerce 3.0版开始,这个功能不再有效.
我应该如何解决这个问题,以使其在3.0及更高版本中运行?
小智 7
现在有一个过滤器。名称是“woocommerce_product_related_products_heading”
所以你可以在你自己的主题functions.php文件中添加一个小片段,如:
add_filter('woocommerce_product_related_products_heading',function(){
return 'My Custom nice related title';
});
Run Code Online (Sandbox Code Playgroud)
试试这个,它和我一起工作
add_filter( 'gettext', 'wps_translate_words_array' );
add_filter( 'ngettext', 'wps_translate_words_array' );
function wps_translate_words_array( $translated ) {
$words = array(
// 'word to translate' = > 'translation'
'Related Products' => 'Check out these related products',
);
$translated = str_ireplace( array_keys($words), $words, $translated );
return $translated;
}
Run Code Online (Sandbox Code Playgroud)
一个简单的替代方法
通过模板文件的主题覆盖 Woocommerce 模板single-product/related.php,您可以直接从以下位置重命名它:
<h2><?php esc_html_e( 'Related products', 'woocommerce' ); ?></h2>
Run Code Online (Sandbox Code Playgroud)
到:
<h2><?php esc_html_e( 'Related Books', 'woocommerce' ); ?></h2>
Run Code Online (Sandbox Code Playgroud)