General Solutions&FAQ

Print

How to limit product titles

Occasionally, too long product titles ruin block layout or you have products where you would like to cut off the title after 4 words and so on. You should just copy/paste this snippet to the end of your functions.php file. This can be done straight on server side (root of theme folder) or through theme editor in WordPress dashboard:

  • Copy this php solution:
function short_woocommerce_product_titles_words( $title, $id ) {
if ( ( is_page() || is_shop() || is_product_tag() || is_product_category() ) && get_post_type( $id ) === 'product' ) {
$title_words = explode(" ", $title);
if ( count($title_words) > 4 ) { // Kicks in if the product title is longer than 4 words
// Shortens the title to 4 wo3rds and adds ellipsis at the end
return implode(" ", array_slice($title_words, 0, 4)) . '...';
} else {
return $title; // If the title isn't longer than 4 words, it will be returned in its full length without the ellipsis
}
} else {
return $title;
}
}
add_filter( 'the_title', 'short_woocommerce_product_titles_words', 10, 2 );
  • Enter wp-dashboard–>appearance–>Theme Editor–>functions.php
  • Paste previously copied php solution.

 

Leave a Reply

Your email address will not be published. Required fields are marked *