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
:
function excerpt($num) {
$limit = $num+1;
$excerpt = explode(' ', get_the_excerpt(), $limit);
array_pop($excerpt);
$excerpt = implode(" ",$excerpt)."... (<a href="" .get_permalink($post->ID) ." ">Read more</a>)";
echo $excerpt;
}
Then, in your theme, use the code <?php excerpt('22'); ?>
to limit the excerpt to 22 characters.
🙂