I have a website in which I need to control the displayed excerpt length.
Some of the posts might have manual excerpt so I can’t use the excerpt_length
filter.
I can, of course, use some kind of substr()
, but was looking for a more elegant solution (if such exists).
Take a look on my answer here: Best Collection of Code for your functions.php file
If I understood your question correctly, it does what you are looking for.
Place this in
functions.php
:Then, in your theme, use the code
<?php excerpt('22'); ?>
to limit the excerpt to 22 characters.🙂
With the recent version of WordPress (v.3.3.0+), you can actually use
wp_trim_words
.See also: https://stackoverflow.com/a/17177847/851045
I’d say just look at how core does it: http://phpxref.ftwr.co.uk/wordpress/wp-includes/formatting.php.source.html#l1840
I took the liberty of putting the code here for ease of copying and pasting.
Simply it can be done as below .
Reference: Codex
Try this: You can control the amount of words the excert outputs with the filter “excerpt_length” below are a couple examples of how you can control the size based on different conditions
EDIT: Crap, I just noticed that you said the filter approach was a no go. oh well this is for other people who get here via google and want this then.
Enter this code in your
functions.php
and use
echo easy excerpt(mylimit)
instead ofthe_excerpt()
It works fine.