Limit the word count in the post title

July 27, 2023 by Frederick in Web development
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_contentthe_excerptthe_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.

Leave a comment


Name

Email


Website


Messagge



Comments

Federico Schiocchet 25 feb, 2018

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Jessica Albuzi 05 Dec, 2016

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Don Jhon 21 feb, 2016

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Sara Molin 14 Mar, 2016

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium

Vaky Yo 11 feb, 2017

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea coSed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium