586 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Provided great contribution to our forums
  • Has been a beta tester for an Envato feature
+4 more
Gareth_Gillman says

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

116 posts
  • Has been part of the Envato Community for over 4 years
  • Has collected 1+ items on Envato Market
  • Located in Europe
  • Sells items exclusively on Envato Market
dejans7 says
PHP explode.

<?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]; 
}
?>
1383 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wpCanyonThemes says

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).

586 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Provided great contribution to our forums
  • Has been a beta tester for an Envato feature
+4 more
Gareth_Gillman says

Thanks so much WP_C, works an absolute treat.

1141 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 100+ items on Envato Market
+2 more
KarmaThemes says
Just wrote it especially for you
That is so romantic! Well, it would be under different circumstances… jk :) It’s very kind of you!
1383 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wpCanyonThemes says

@Gareth – Welcome :)

@glossycat – Haha :) Want me to write something especially for you? :P

1141 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 1+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 100+ items on Envato Market
+2 more
KarmaThemes says

Awwww! :) The love poems of the 21st century!

1383 posts
  • Has been part of the Envato Community for over 4 years
  • Has referred 10+ members
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wpCanyonThemes says

lol :)

by
by
by
by
by
by