dongshenjie3055 2017-09-25 19:16
浏览 85
已采纳

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

Let's say my product title is:

Brown Colored Leather Shoes

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

Brown Colored Leather
Shoes

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 want it to look like this

Brown Colored
Leather Shoes

I found these references:

Is it possible to add a line break to WC products long titles?

  • 写回答

1条回答 默认 最新

  • dongmeng1868 2017-09-25 19:41
    关注

    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;
    }
    

    Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

    Code is tested on Woocommerce 3+ and works.

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错