0

`

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;
    }
}

`

I have this code to limit product title characters in homepage:

i want to modify it to limit the characters of the description in the single product page of woocommerce. How to do? Thank you

I tried to replace 'the_title' with 'description' but it doesn't work

Progman
  • 16,827
  • 6
  • 33
  • 48
Linux
  • 1
  • 1

1 Answers1

0

I believe you weren't using the correct filter. Try the below code. Change values depending on what you need. Here is the reference link: 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; 
}; 
JayDev95
  • 777
  • 2
  • 5
  • 18
  • Thanks, this code is for the short description.. I want to apply it to long description (normal, full) – Linux Dec 24 '22 at 16:29
  • Found it: https://stackoverflow.com/questions/50653135/reduce-product-long-description-in-woocommerce – Linux Dec 24 '22 at 16:39