skip to Main Content

I can’t get rid of the ellipsis and the hyphen. Is there anyone who can help?

Using the code below I was finally able to remove the Stock Codes at the end of the Product Title. But I still couldn’t get rid of the hyphen and there are three dots at the end of the product titles.

add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
function shorten_woo_product_title( $title, $id ) {
    if ( get_post_type( $id ) === 'product' ) {
        return wp_trim_words( $title, -1 ); // change last number to the number of WORDS you want
    } else {
        return $title;
    }
}

I had this;

T-shirt – 953854864

Now there is this;

T-shirt -…

2

Answers


  1. Chosen as BEST ANSWER
    add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
            function shorten_woo_product_title( $title, $id ) {
            if ( get_post_type( $id ) === 'product' ) {
            // Removes the specified characters and replaces consecutive spaces with a single space
            $title = preg_replace( "/[s-.0-9&#;]+/", ' ', $title );
            // Clears spaces at the beginning and end of the header
            $title = trim( $title );
            }return $title;
            }
    

    Thank you very much. For some reason the code you wrote didn't work on my site. This is how I got rid of the characters I don't want; @LoicTheAztec


  2. Try instead the following using preg_replace() to remove, for example " – 953854864" substring from "T-shirt – 953854864" product title:

    add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
    function shorten_woo_product_title( $title, $id ) {
        if ( get_post_type( $id ) === 'product' ) {
            $title = preg_replace( "~(s-sd+)$~", '', $title );
        }
        return $title;
    }
    

    It should work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search