【发布时间】:2021-03-27 02:21:12
【问题描述】:
我正在尝试限制我们的 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;
}
}
它有效,但我希望它在每个产品标题的末尾显示“...”。
【问题讨论】:
-
将
return substr( $title, 0, 30);替换为return substr( $title, 0, 30) . '…'; -
还有一个问题:如何从短于本代码中允许的最大长度(在这种情况下为 30)的产品标题中删除“...”?
标签: php wordpress woocommerce hook-woocommerce string-length