问题
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