将文本字符串附加到 WooCommerce 产品标题

Append a text string to WooCommerce product title

<div class="woocommerce-loop-product__title"><? wc_get_template('single-product/title.php');?>

我试图让这个字符串说:"Product Name More Info"

我可以很好地获取产品名称部分,但是如何将 'more info' 连接到此代码以使其正确输出?我尝试的每一次回声尝试都会破坏我的网站

如果您尝试将产品名称标题与字符串连接起来,则可以使用 woocommerce_shop_loop_item_title 挂钩。

remove_action( 'woocommerce_shop_loop_item_title','woocommerce_template_loop_product_title', 10 );
function change_product_title() {
    $additional_text = ' More info';
    echo '<h2 class="woocommerce-loop-product__title">' . get_the_title() .$additional_text.'</h2>';
}
add_action('woocommerce_shop_loop_item_title','change_product_title');

或者如果您想更改单个产品页面中的产品标题,

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );

function woocommerce_template_single_title_custom(){
    $additional_text = ' More Info';
    the_title( '<h3 class="product_title entry-title">', $additional_text.'</h3>' );
}
add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title_custom', 5);

the_title函数的语法是

the_title( $before, $after, $echo );

如果您查看模板 single-product/title.php 代码,您基本上就是这样:

the_title( '<h1 class="product_title entry-title">', '</h1>' );

WordPress 函数 the_title() 回显产品标题,因此如果要将 "More Info" 附加到标题,则不能再次使用回显。您将改用此行:

<div class="woocommerce-loop-product__title"><? the_title( '<h1 class="product_title entry-title">', ' More Info</h1>' ); ?>

或者更简洁的方式。

<div class="woocommerce-loop-product__title"><? the_title( '<h1 class="product_title entry-title">', ' '. __("More Info") . '</h1>' ); ?>

已测试并有效。

一个很好的问题,我花了好几天时间和大量的 Stack Overflowing 才找到适合我的解决方案。

使用@melvin 的回答(第二个解决方案),我做了一些更改以避免标题和 $additional_text 重复。我还包含了一个双换行符并应用了 class.

用您喜欢的文字替换我们的评论。双 'break' 纯粹是为了样式,如果需要可以删除。

这是我的解决方案(添加到 child 主题 - functions.php),经过测试并在 WooCommerce v.5.51 上运行:

remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );

function woocommerce_template_single_title_custom(){
    $additional_text = ' OUR REVIEW OF '."<br><br>";
   
echo "<span style='font-size: 30px; color: #00a4cc; vertical-align: top;'>".$additional_text."</span>";
}

add_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title_custom', 5);