在Woocommerce产品标题中添加换行符 [英] Add a line break in Woocommerce Product Titles

查看:169
本文介绍了在Woocommerce产品标题中添加换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说我的产品标题是:

Let's say my product title is:

棕色皮鞋

但是根据我的列宽,标题看起来像这样:

But based on my column width the title looks like this:

棕色皮革
鞋子

Brown Colored Leather
Shoes

我知道可以进行字符替换,以便后端中的"|"成为换行符<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.

我希望它看起来像这样

棕色
皮鞋

Brown Colored
Leather Shoes

我找到了这些参考文献:

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?

推荐答案

使用挂钩在 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_the_title', 10, 2 );
function custom_the_title( $title, $post_id ){
    $post_type = get_post_field( 'post_type', $post_id, true );
    if( $post_type == 'product' || $post_type == '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() (以及许多其他)

这篇关于在Woocommerce产品标题中添加换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆