限制字符描述单个产品页面wooccommerce



`

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, 20) . '…'; // change last number to the number of characters you want
} else {
return $title;
}
}

`

我有这个代码来限制主页中的产品标题字符:

我想对它进行修改,以限制woocommerce的单个产品页面中描述的字符。怎么办?谢谢

我试图用"description"替换"the_title",但不起作用

我相信您没有使用正确的过滤器。请尝试以下代码。根据需要更改值。以下是参考链接:https://wpbeaches.com/limit-the-words-in-woocommerce-product-short-description/

<?php // <~ don't add me in
add_filter( 'woocommerce_short_description', 'prefix_filter_woocommerce_short_description' );
/**
* Limit WooCommerce Short Description Field
*/
function prefix_filter_woocommerce_short_description( $post_post_excerpt ) { 
// make filter magic happen here... 
if(! is_product() ) { // add in conditionals
$text = $post_post_excerpt; 
$words = 10; // change word length
$more = ' […]'; // add a more cta

$post_post_excerpt = wp_trim_words( $text, $words, $more );
}
return $post_post_excerpt; 
}; 

最新更新