I am trying to limit the blog title in a slider, but all the options I have tried are reducing by character and not word.
How can I do it for individual words?
Cheers
<?php //For example our title is: "WordPress Post With Long Title"
$new_title = explode ( " ", the_title() );
echo $new_title[0]; // WordPress
echo $new_title[1]; // Post
echo $new_title[2]; // With
// and so on....
//Or if you want to echo first, lets say, 10 words, you can do it like this withouth writing 10 lines of code
for ($i = 1; $i <= 10; $i++) {
echo $new_title[$i];
}
?>
Just wrote it especially for you 
add_filter('the_title', 'gareth_title_word_limit');
function gareth_title_word_limit($title = ''){
$word_limit = 2; //The amount of words to return
return implode(' ',array_slice(str_word_count($title,1),0,$word_limit));
}
Haven’t tested thou, let me know if it works. 
Edit: Ohh wait, you want to limit it only on the slider. I’ll leave the previous code maybe someone needs it in future. So just lose the add_filter and a small change to the function name for better descriptional purpose 
function gareth_word_limit($title = '', $word_limit = 5){
return implode(' ',array_slice(str_word_count($title,1),0,$word_limit));
}
And then when you’re calling the title in the slider do it like this.
echo gareth_word_limit(get_the_title(), 3);
Edit: Improved. Added the word count as a parameter (default 5).
Thanks so much WP_C, works an absolute treat.
Just wrote it especially for youThat is so romantic! Well, it would be under different circumstances… jk
It’s very kind of you!@Gareth – Welcome 
@glossycat – Haha
Want me to write something especially for you? 
Awwww!
The love poems of the 21st century!
lol 
