[英]Limit WooCommerce product title to a specific length (and add '...')
I'm trying to limit the number of characters our WooCommerce product titles have, I found this PHP code:我试图限制我们的 WooCommerce 产品标题的字符数,我发现了这个 PHP 代码:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' ) {
return substr( $title, 0, 30); // change last number to the number of characters you want
} else {
return $title;
}
}
It works but I would like it to show the "..." at the end of each product title.它有效,但我希望它在每个产品标题的末尾显示“...”。
You can use strlen()
php function to target specific product title length to append …
to shortened title when product title is over a specific length:您可以使用
strlen()
php function 将特定产品标题长度定位到 append …
当产品标题超过特定长度时缩短标题:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {
return substr( $title, 0, 30) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}
Code goes in functions.php file of the active child theme (or active theme).代码进入活动子主题(或活动主题)的functions.php文件。 Tested and works.
测试和工作。
I use this code and change the character numbers to 40 but then it goes to 2nd line and then the alignment of add to cart button disturb, it get changed from other products alignmen.我使用此代码并将字符编号更改为 40,但随后进入第二行,然后是 alignment 的添加到购物车按钮干扰,它与其他产品对齐更改。
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 ); add_filter('the_title', 'shorten_woo_product_title', 10, 2); function shorten_woo_product_title( $title, $id ) { if (, is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) { return substr( $title, 0. 30);
function short_woo_product_title( $title, $id ) { if (, is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) { return substr( $title, 0. 30); '…';
'……'; // change last number to the number of characters you want } else { return $title;
// 将最后一个数字更改为您想要的字符数 } else { return $title; } }
} }
You could do this with CSS by adding the following style attributes to the element containing the content you want to ellipsize.您可以使用 CSS 执行此操作,方法是将以下样式属性添加到包含要省略的内容的元素。 This would ensure that the content always displays to the edge of the element, without over-flowing.
这将确保内容始终显示到元素的边缘,而不会溢出。
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
This method should be more performant than the JS solution and when implementing functionality like this, I'd always choose a pure-CSS solution if possible, before reverting to Javascript or trimming the string on the backend.这种方法应该比 JS 解决方案性能更高,在实现这样的功能时,如果可能,我总是选择纯 CSS 解决方案,然后再恢复到 Javascript 或修剪后端的字符串。
white-space: nowrap;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
text-overflow: ellipsis;
overflow: hidden;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.