添加在Woocommerce产品标题换行
问题描述:
比方说我的产品名称是:添加在Woocommerce产品标题换行
Brown Colored Leather Shoes
但基于我的列宽的标题是这样的:
Brown Colored Leather
Shoes
我知道这是可以做到的替换字符以使后端中的"|"
成为换行符<br/>
。但我不知道如何。
我希望它看起来像这样
Brown Colored
Leather Shoes
我发现这些引用:
Adding a break to product title on shop pages for a certain length
https://medium.com/@clarklab/add-a-line-break-in-a-wordpress-title-b3aeff346037
是否可以为WC产品长标题添加换行符?
答
使用the_title
过滤钩子钩住这个自定义函数将做的工作(更换管道字符|
通过在产品标题的<br>
):
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文件),或者也可以任何插件文件。
代码在Woocommerce 3+上测试并正常工作。
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)…
没有。我一定在做错事:(https://imgur.com/ki1Avuw添加到我的孩子主题btw。 – coes
:(是的,我看到它适用于你。你看到任何可能看起来与我的截图错?谢谢您花时间回答。 – coes
好的,我得到这个去做一些事情! – coes