I'm trying to limit WordPress post excerpt and I've tried some ways to do it but all of them was not the stuff that I need, to say concisely I want to limit post excerpt with a number that I use everywhere with differences.
For example, I need to make something I use it like this:
<?php the_excerpt('30') ?>
with this part of the code, I wanna limit my excerpt to 30 chars and in another place, I wanna use a different value like:
<?php the_excerpt('150') ?>
Is it in WordPress?
You can shorten the excerpt with something like this:
function shorten( $s, $num, $dots ) {
if ( $num < mb_strlen( $s ) ) {
$fs = mb_substr( $s, 0, $num );
for ( $i = mb_strlen( $fs ); $i >= 0; $i -- ) {
if ( mb_substr( $fs, $i, 1 ) == ' ' ) {
return mb_substr( $fs, 0, $i + 1 ) . $dots;
}
}
return $fs . $dots;
} else {
return $s;
}
}
You can then call it like that: shorten(get_the_excerpt(), 40, '...')
(replacing the dots with something else or nothing, if you like).
(Source)