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

在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 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退