在 Woocommerce 产品标题中添加换行符

coe*_*oes 1 php wordpress product title woocommerce

假设我的产品标题是:

棕色皮鞋

但根据我的列宽,标题如下所示:

棕色
皮鞋

我知道可以进行字符替换,以便"|"后端的 a 变成换行符<br/>。但我不知道怎么办。

我希望它看起来像这样

棕色
皮鞋

我找到了这些参考资料:

WC 产品长标题是否可以添加换行符?

Loi*_*tec 6

2020 修订版-仍然适用于 Wordpress 5.5.1 和 WooCommerce 4.4.1

\n

使用挂钩在过滤器挂钩中的自定义函数the_title可以完成这项工作(|<br>产品标题中的 a 替换管道字符):

\n
add_filter( 'the_title', 'custom_product_title', 10, 2 );\nfunction custom_product_title( $title, $post_id ){\n    $post_type = get_post_field( 'post_type', $post_id, true ); \n\n    if( in_array( $post_type, array( 'product', 'product_variation') ) ) {\n        $title = str_replace( '|', '<br/>', $title ); // we replace "|" by "<br>"\n    }\n    return $title;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

代码位于活动子主题(或主题)的 function.php 文件中或任何插件文件中。

\n

代码在 Woocommerce 3+ 上进行了测试并且可以运行。

\n
\n

现在,您可以使用 WC 条件标记定位不同的产品页面,如is_product()is_shop()is_product_category()is_product_tag() 以及许多其他) \xe2\x80\xa6

\n
\n