Just slight modification to the code above by Roy Ho.
Using is_shop() // Function is_shop() – Returns true when viewing the product type archive (shop).
Thus if you are on a page and you have title longer, is_shop() function will not return false and therefore your title will still be longer than necessary and may make your webpage not interesting.
To fix this issue, you can change the function above from :
is_shop() to is_page()
is_page() will ensure the rest code works on any ‘product’ display page, because where are specifing the ‘product’ in the code.
The end result will look like this;
=====code begin=======
add_filter( ‘the_title’, ‘shorten_my_title’, 10, 2 );
function shorten_my_title( $title, $id ) {
if ( is_page() && get_post_type( $id ) === ‘product’ && strlen( $title ) > 50 ) {
return substr( $title, 0, 50 ) . ‘…’;
} else {
return $title;
}
}
=====code end=======
Copy the above code and paste it in your theme functions.php it should do the work.
NB: Make a backup copy of your original functions.php just in case you have to revert back to it.