我有这段代码,试图将“相关产品”翻译成“这些产品与产品名称搭配得很好”。
这是我的代码:
add_filter( 'gettext', 'change_related_products_title', 10, 3 );
function change_related_products_title( $translated, $text, $domain ) {
$ptitle = get_page_by_title( 'Product Title', OBJECT, 'product' );
if( $text === 'Related products' && $domain === 'woocommerce' ){
$translated = esc_html__( 'These go well with '.$ptitle.' ', $domain);
}
return $translated;
}
它所显示的只是“这些搭配得很好”,仅此而已。请帮助。
代替get_page_by_title()
use get_the_title()
like:
add_filter( 'gettext', 'change_related_products_title', 10, 3 );
function change_related_products_title( $translated, $text, $domain ) {
if( $text === 'Related products' && $domain === 'woocommerce' ){
$translated = esc_html__( 'These go well with', $domain ) . ' ' . esc_html( get_the_title() );
}
return $translated;
}
代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)