添加在Woocommerce产品标题换行

问题描述:

比方说我的产品名称是:添加在Woocommerce产品标题换行

Brown Colored Leather Shoes

但基于我的列宽的标题是这样的:

Brown Colored Leather
Shoes

我知道这是可以做到的替换字符以使后端中的"|"成为换行符<br/>。但我不知道如何。

我希望它看起来像这样

Brown Colored
Leather Shoes

我发现这些引用:

是否可以为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() or is_product_tag()(and many others)

+0

没有。我一定在做错事:(https://imgur.com/ki1Avuw添加到我的孩子主题btw。 – coes

+0

:(是的,我看到它适用于你。你看到任何可能看起来与我的截图错?谢谢您花时间回答。 – coes

+0

好的,我得到这个去做一些事情! – coes

当你写产品的标题时,你可以添加非易碎空间来控制它的标题。为了您的例子中,你可以这样做:

Brown&nbsp;Colored Leather&nbsp;Shoes 

的宽度小,你将有像你想

+0

试过了。这是 ,但它没有工作:( – coes

+0

你能告诉我该网站的链接? –

+0

是的。这是安全的发布链接在这里吗?或者我可以DM吗? – coes