Does long title bother you? Are you trying to find solution to trim the title and display some limited words ?
Let’s say if your title is longer than 8 words, you want to display first 8 words and ignore the rest. Solution, to trim the title could be count the number of words in the title and if the title consist of more than 8 words, extract 8 words from the title and display it , otherwise displayed the title as it is.
Steps 1 : Create function that returns the first 8 words from the title/sentence, place it anywhere in your function.php or helpers.php files
function get_words($sentence, $count = 8) {
preg_match("/(?:\w+(?:\W+|$)){0,$count}/", $sentence, $matches);
return $matches[0];
}
Step 2 : Count the number of words in the title and use the function in step 1, to extract 8 words form the title. If the title has less than 8 words, do nothing just display the title.
$count = str_word_count(get_the_title());
if($count > 8) {
$title = get_words(get_the_title(),8);
}else {
$title = get_the_title();
}
Views: 30