Title not shortened in featured products

On the featured products – for example on product page below product details
https://prnt.sc/p8n4n9

https://petbox.pl/sklep/calista-adult-cat/

titles are not shortened on shop categories and subcategories. Can You help me with that? I shortened titles with code in functions.php

I want to shortened title in featured products

add_filter( ‘the_title’, ‘shorten_woo_product_title’, 10, 2 );
function shorten_woo_product_title( $title, $id ) {
if ( is_shop() && get_post_type( $id ) === ‘product’ ) {
return wp_trim_words( $title, 4 ); // change last number to the number of WORDS you want
}

else {
return wp_trim_words( $title);
}
}

add_filter( ‘the_title’, ‘shorten_woo_product_title_product_cat’, 10, 2 );
function shorten_woo_product_title_product_cat( $title, $id ) {
if ( is_product_category() ) {
return wp_trim_words( $title, 4 ); // change last number to the number of WORDS you want
}
return $title;
}

  • Kasia Swiderska
    • Support nomad

    Hello Emilia ,

    On the featured products – for example on product page below product details
    https://prnt.sc/p8n4n9

    Do you mean related products on the product page? From what I see problem might be here:

    if ( is_shop() && get_post_type( $id ) === ‘product’ ) {

    since it will check if both conditions are true, so if it’s shop page and post type is a product. Try using || instead of &&. Also, you should be able to use is_product

    titles are not shortened on shop categories and subcategories.

    I can see them shortened correctly:

    kind regards,
    Kasia

  • Emilia
    • WPMU DEV Initiate

    Can You provide me the full code for shortened titles in featured products? I am afraid that I do not understand how to correct the code and where. Thx. I know that in the categories is ok but still featured products have to be change

  • Rupok
    • Support Ninja

    Hi Emilia,

    The following code should truncate the product title everywhere on your shop page.

    <?php
    add_filter( 'the_title', 'shorten_woo_product_title', 10, 2 );
    function shorten_woo_product_title( $title, $id ) {
    	if ( is_shop() && get_post_type( $id ) === 'product' ) {
    		return wp_trim_words( $title, 4 ); // change last number to the number of words you want
    	} else {
    		return $title;
    	}
    }

    This code should show 4 words. If you want to change the count of words, you can change the number in this line:

    return wp_trim_words( $title, 4 );

    Please let us know if I missed anything or if I understood your query wrong. We will assist further.

    Regards,
    Rupok

  • Kasia Swiderska
    • Support nomad

    Hello Emilia ,

    Please try this code:

    add_filter( 'the_title', 'wpmudev_trimtitles', 10, 2 );
      
    function wpmudev_trimtitles( $title ) {
     
    global $woocommerce_loop;
     
    if ( is_product() && $woocommerce_loop['name'] == 'related' ) {
    
    $title = wp_trim_words( $title , 4 , '...');
    
    }
     
    return $title;
    }
    

    I have tested it on my lab site and this one trims related products titles.

    kind regards,
    Kasia

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.