Con*_*tin 2 php wordpress string-length woocommerce hook-woocommerce
我试图限制我们的 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;
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但我希望它在每个产品标题的末尾显示“...”。
您可以使用strlen()php 函数来定位特定的产品标题长度,以\xe2\x80\xa6在产品标题超过特定长度时附加到缩短的标题:
add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );\nfunction shorten_woo_product_title( $title, $id ) {\n if ( ! is_singular( array( 'product' ) ) && get_post_type( $id ) === 'product' && strlen( $title ) > 30 ) {\n return substr( $title, 0, 30) . '\xe2\x80\xa6'; // change last number to the number of characters you want\n } else {\n return $title;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n代码位于活动子主题(或活动主题)的functions.php 文件中。经过测试并有效。
\n| 归档时间: |
|
| 查看次数: |
7788 次 |
| 最近记录: |