WooCommerce:如何缩短产品标题, how to limit product title length.

可以通过CSS 或者PHP 代码来实现,有以下几个方法参考:

1. BY CSS:只显示一行:

// Note: this is simple CSS that can be placed in your custom.css file
// This CSS also adds 3 dots ... at the end of each product title
 
.woocommerce ul.products li.product h3 {
   overflow: hidden;
   text-overflow: ellipsis;
   white-space: nowrap;
}

注:标题的CLASS 根据自己实际情况修改。 

2. PHP 方法:

一,限制字符数:

add_filter( 'the_title', 'bbloomer_shorten_woo_product_title', 9999, 2 );
 
function bbloomer_shorten_woo_product_title( $title, $id ) {
   if ( is_shop() && get_post_type( $id ) === 'product' ) {
      return substr( $title, 0, 15 ); // last number = characters
   } else {
      return $title;
   }
}

代码可添加到 functions.php

二,限制字数:

function bbloomer_shorten_woo_product_title( $title, $id ) {
   if ( is_shop() && get_post_type( $id ) === 'product' ) {
      return wp_trim_words( $title, 4 ); // last number = words
   } else {
      return $title;
   }
}

代码可添加到 functions.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值