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

Change Related Products Heading adding the product name in WooCommerce

我有这段代码,它试图将 "Related products" 翻译成 "These will go well with 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;
}

它只显示 "These go well with",仅此而已。请帮忙

而不是 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 文件。已测试并有效。