Change Related Products Heading adding the product name in WooCommerce

自闭症网瘾萝莉.ら 提交于 2021-02-19 06:48:04

问题


I have this code which is an attempt in translating "Related products" into "These will go well with PRODUCT NAME".

Here's my code:

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

All it shows is "These go well with" and nothing more. Help please.


回答1:


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

Code goes in functions.php file of your active child theme (or active theme). Tested and works.



来源:https://stackoverflow.com/questions/56138104/change-related-products-heading-adding-the-product-name-in-woocommerce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!