Limit the word count in the post title
Are you looking for Limit the word count in the post title to trim your Post Title Length in WordPress? While there’s probably a plugin for this, we have created a quick code snippet that allows you to use wp_trim_words to trim your text in WordPress.
WordPress 3.3+ has a core function called
wp_trim_words()
. This function will trim text to a specified number of words and return the result. Adding this snippet to your WordPress loop will let you easily trim the number of words displayed within the_content
, the_excerpt
, the_title
or any other value you pass to this core function.
Instructions:
All you have to do is add this code to your theme’s PHP file where you want to show the title:
If continue sign "..." needed at the end of title
<?php echo wp_trim_words( get_the_title(), 10, '...' ); ?>
If continue sign "..." not needed at the end of title
<?php echo wp_trim_words( get_the_title(), 10 ); ?>
Parameters:
- $text
- (string) (required) Text to trim
- Default: None
- $num_words
- (integer) (optional) Number of words
- Default: 55
- $more
- (string) (optional) What to append if $text needs to be trimmed.