更改相关产品标题,在WooCommerce中添加产品名称

时间:2019-05-14 20:25:42

标签: php wordpress woocommerce product gettext

我有这段代码,试图将“相关产品”翻译成“这些将与PRODUCT NAME一起使用”。

这是我的代码:

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;
}

它显示的全部是“这些都很好”,仅此而已。请帮助。

1 个答案:

答案 0 :(得分:0)

使用get_page_by_title()代替get_the_title(),例如:

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文件中。经过测试,可以正常工作。

相关问题