I am trying to add an excerpt length of e.g. 50 to the following Understap theme code. https://github.com/understrap/understrap/blob/main/inc/extras.php
The specific code is:
add_filter( 'wp_trim_excerpt', 'understrap_all_excerpts_get_more_link' );
if ( ! function_exists( 'understrap_all_excerpts_get_more_link' ) ) {
/**
* Adds a custom read more link to all excerpts, manually or automatically generated
*
* @param string $post_excerpt Posts's excerpt.
*
* @return string
*/
function understrap_all_excerpts_get_more_link( $post_excerpt ) {
if ( is_admin() || ! get_the_ID() ) {
return $post_excerpt;
}
$permalink = esc_url( get_permalink( (int) get_the_ID() ) ); // @phpstan-ignore-line -- post exists
return $post_excerpt . ' [...]<p><a href="' . $permalink . '">' . __(
'Read More...',
'understrap'
) . '<span > from ' . get_the_title( get_the_ID() ) . '</span></a></p>';
}
}
Hoping someone very knowledgeable of WordPress can see a convenient way of integrating excerpt_length into it. https://developer.wordpress.org/reference/hooks/excerpt_length/
Thanks
CodePudding user response:
add_filter( 'excerpt_length', 'understrap_custom_excerpt_length', 999 );
function understrap_custom_excerpt_length( $length ) {
return 50;
}
You can add this code before the add_filter( 'wp_trim_excerpt', 'understrap_all_excerpts_get_more_link' );
line in your extras.php file.
Good luck !
CodePudding user response:
This luckily works for me:
function custom_excerpt_length($excerpt) {
if (has_excerpt()) {
$excerpt = wp_trim_words(get_the_excerpt(), apply_filters("excerpt_length", 30));
}
return $excerpt;
}
add_filter("the_excerpt", "custom_excerpt_length", 999);
stumbled across it at https://wordpress.stackexchange.com/questions/139953/excerpt-length-not-working
Thanks