Archived topic
Excerpt Field Length
3 replies · Started by Andy on May 15, 2020
Hi,
Is there a filter for setting the max number of words displayed from the Excerpt Field? (not the excerpt auto-generated by Wordpress/Theme)
Hi there,
may i ask why you want to do that when you could just type a shorter excerpt?
Only because they are job vacancy posts and as theres likely to be alot of them I'm not sure the client would want to check how many words they are writing everytime they publish a post.
I suppose you could re-purpose this function:
https://docs.generatepress.com/article/activating-read-custom-excerpt/#read-more-text
Something like this:
add_filter( 'wp_trim_excerpt', 'tu_excerpt_metabox_more' );
function tu_excerpt_metabox_more( $excerpt ) {
$output = $excerpt;
if ( has_excerpt() ) {
$output = sprintf( '%1$s <a href="%2$s">%3$s</a>',
wp_trim_words( $excerpt, 15),
get_permalink(),
__( 'Read more', 'generatepress' )
);
}
return $output;
}
This wp_trim_words( $excerpt, 15) is what sets the number of words to display