在 Woocommerce 产品标题中添加换行符 [英] Add a line break in Woocommerce Product Titles
问题描述
假设我的产品标题是:
棕色皮鞋
但根据我的列宽,标题如下所示:
But based on my column width the title looks like this:
棕色皮革
鞋子
我知道可以进行字符替换,以便后端中的 "|"
变成换行符 <br/>
.但我不知道如何.
I know it's possible to do a character replacement so that a "|"
in the backend becomes a line break <br/>
. But I don't know how.
我希望它看起来像这样
棕色
皮鞋
我找到了这些参考资料:
I found these references:
https:///medium.com/@clarklab/add-a-line-break-in-a-wordpress-title-b3aeff346037
WC 产品的长标题是否可以添加换行符?
Is it possible to add a line break to WC products long titles?
推荐答案
2020 修订版 - WooCommerce 4.4.1 仍然适用于 Wordpress 5.5.1
使用挂在 the_title
过滤器钩子中的这个自定义函数将完成这项工作(用 < 替换管道字符
在产品标题中):|
br>
Using this custom function hooked in the_title
filter hook will do the job (replacing a pipe character |
by a <br>
in product titles):
add_filter( 'the_title', 'custom_product_title', 10, 2 );
function custom_product_title( $title, $post_id ){
$post_type = get_post_field( 'post_type', $post_id, true );
if( in_array( $post_type, array( 'product', 'product_variation') ) ) {
$title = str_replace( '|', '<br/>', $title ); // we replace "|" by "<br>"
}
return $title;
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码已在 Woocommerce 3+ 上进行测试并有效.
Code is tested on Woocommerce 3+ and works.
现在您可以使用 WC 条件标签将不同的产品页面定位为 is_product()
、is_shop()
、is_product_category()
或 is_product_tag()
(和许多其他的)...
Now you can target different product pages with the WC conditionals tags as
is_product()
,is_shop()
,is_product_category()
oris_product_tag()
(and many others)…
这篇关于在 Woocommerce 产品标题中添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!