Support » Fixing WordPress » shortening product titles shortens all titles

  • Hi – I think a snippet of code in my functions file has “expanded” it’s reach after a WordPress update (not entirely sure how long this has been happening though).

    My WooCommerce store has widely varying product titles, so we impose this function to shorten and add an ellipses at the end:

    
    add_filter( 'the_title', 'shorten_my_title', 10, 2 );
    
    // Shorten Post Titles (mostly for long-assed product names)
    function shorten_my_title( $title, $id ) {
    if ( is_shop() || is_product_tag() || is_product_category() && get_post_type( $id ) === 'product' && strlen( $title ) > 40 ) {
    return substr( $title, 0, 40 ) . '...';
    } else {
    return $title;
    }
    }
    

    This worked great for over a year, but now we are seeing that even our mega menu links are getting shortened. Basically it’s run amok and we can’t figure out what may have changed in WordPress (or our own user error!) that would now cause this function above to affect the menu.

    Any idea what’s wrong or how we might narrow this function so it doesn’t affect our menu at all?

    Thanks

    Bill

Viewing 4 replies - 1 through 4 (of 4 total)
  • Perhaps change ‘the_title’ on first line with ‘woocommerce_product_title’? (however, if your products are made as WordPress posts… doubt it will work then).

    did you mean:

    if ( ( is_shop() || is_product_tag() || is_product_category() ) && ( get_post_type( $id ) === 'product' && strlen( $title ) > 40 )) {

    I meant:

    
    add_filter( 'woocommerce_product_title', 'shorten_my_title', 10, 2 );
    
    // Shorten Post Titles (mostly for long-assed product names)
    function shorten_my_title( $title, $id ) {
    if ( is_shop() || is_product_tag() || is_product_category() && get_post_type( $id ) === 'product' && strlen( $title ) > 40 ) {
    return substr( $title, 0, 40 ) . '...';
    } else {
    return $title;
    }
    }
    

    consider to add in_the_loop() to your conditional statement; https://codex.wordpress.org/Function_Reference/in_the_loop

    what theme or what plugin are you using for the woocommerce?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘shortening product titles shortens all titles’ is closed to new replies.