• Resolved Overflame

    (@overflame)


    I found this code:

    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 want to add the “…” at the end of the product title and I don’t know how.

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Rynald0s

    (@rynald0s)

    Automattic Happiness Engineer

    Thread Starter Overflame

    (@overflame)

    Already tried that, but we don’t need by ‘words’. We need by ‘characters’.

    Plugin Support Dani F. a11n

    (@danielinhou)

    Hi there!

    Thanks for getting back to us. You could use this code instead:

    
    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 mb_strimwidth($title, 0, 10, '...');// change last number to the number of words you want
        } else {
            return $title;
        }
    }
    
    

    I have tested this code on my site and it works so I am going to mark this thread as resolved – if you have any further questions, you can start a new one.

    Cheers!

    Hi there @overflame,

    Use this code to limit by characters:

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

    Hope it helps!

    Thank Dani F. a11n (@danielinhou)
    It great code!

    Hi all , I need help can any body help me please, My 1 product title is around 35words, i change the limit of characters to 40 its showing on product but it goes to 2nd line and then the alignment of add to cart button get disturb, it is not align to the 2nd product add to car button. its litle bit down. how can i make it correct, the name should also be visible but add to cart shouldnt disturb.
    https://www.freshtodoor.ae , on mobile view and in product category archive page giving the problem.

    Hi @freshtodoouae

    Since this thread has been closed, please create your own topic here:
    https://www.ads-software.com/support/plugin/woocommerce/

    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;
        }
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Limit Woocommerce Product Title Number of Characters’ is closed to new replies.