I have code in functions.php:
function string_limit_words($string, $word_limit)
{
$words = explode(' ', $string, ($word_limit + 1));
if(count($words) > $word_limit)
array_pop($words);
return implode(' ', $words);
}
but i need to limit excerpt in number of characters,
could you help me with that?
I used this code in one of my last projects:
I got it from here:
http://wordpress.org/support/topic/limit-excerpt-length-by-characters
https://stackoverflow.com/questions/10923955/make-function-that-limits-text-not-show-last-punctuation-mark
It has the advantage of not allowing punctuation on the end and ending with the last complete word
Using the filters as suggested by @medhamza7 or @bainternet or @fuxia is preferable.
Use the function
utf8_truncate()
from this answer and fight your way throughwp_trim_excerpt()
.Sample code, not tested:
WordPress has a filter for that which is conveniently named excerpt_length and it accepts a number of chars so:
change 50 to whatever limit you want.
Update per @toscho comment:
that is the solution above is for words as well and not for chars so here is a quick one:
For a better way, you can use the
get_the_excerpt
filter:Change the
$limit=140
to the number of characters you want. Also if you want in different way:That will make avoid any conflict like existing name of function
get_excerpt
.