简体   繁体   English

将 WooCommerce 产品标题限制为特定长度(并添加“...”)

[英]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.

相关问题 如何分别限制每个标题的WooCommerce产品标题长度? - How to limit WooCommerce Product title length for every title separately? 在 Woocommerce 单个产品的标题前添加链接的特定产品属性 - Add linked specific product attribute before title on Woocommerce single products 在 Woocommerce 单个产品的标题后添加特定的产品属性 - Add specific product attribute after title on Woocommerce single products 限制 Woocommerce 中的产品简短描述长度 - Limit product short description length in Woocommerce 尝试在woocommerce产品名称中添加带有跨度的“ Title:” - Trying to add “Title:” with span to woocommerce product name 在WooCommerce产品图片下方添加标题 - Add title underneath WooCommerce product gallery images 在 Woocommerce 存档页面中的产品标题下显示特定的产品属性 - Display specific product attributes under product title in Woocommerce archive pages 在基于父项的Woocommerce产品上显示特定的产品子类别标题 - Display specific product child category title on Woocommerce product based on parent WooCommerce | 在存档页面上的产品标题下添加产品副标题 - WooCommerce | Add product subheading underneath product title on archive page 将产品类别添加到Woocommerce中的Cross sales产品标题中 - Add the product category to Cross sells product title in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM