Exactly “where” may differ depending on your theme setup. However, there are some global-ish ways to do this if you want to tackle it that way.
add_filter( 'the_title', 'my_trim_words' );
function my_trim_words( $title )
{
// limit to ten words
return wp_trim_words( $title, 10, '' );
}
If you’d like to do this for a particular post type, you can do it like this:
add_filter( 'the_title', 'my_trim_words_by_post_type', 10, 2 );
function my_trim_words_by_post_type( $title, $post_id )
{
$post_type = get_post_type( $post_id );
if( 'product' == $post_type ) {
return $title;
// limit to ten words
return wp_trim_words( $title, 10, '' );
}
If it’s within the theme templates (frontend files) that you’re wanting to do this in very specific areas, you can use it this way in your templates:
<?php echo wp_trim_words( get_the_title(), 5 ); ?>