-
tech@FHR
I used the Pattern Library to create a Query Loop for 3 specific posts. One of those posts has an excerpt containing 48 words, the other two don’t have any words in the excerpt. The Query Loop template has a headline using the Post excerpt and the length is set to 24. When viewed in the editor all 3 posts show 24 words, but when viewed on the website front-end, the posts that don’t have excerpts show 24 words from the post content, but the post that does have an excerpt displays the entire excerpt, it isn’t limited to 24 words, all 48 are displayed.
Can anyone explain why this is happening, or preferably how to fix it, please?
Thanks!
BTW I’m using GeneratePress Premium and GenerateBlocks Pro (all up to date).
-
Hi there,
That’s expected behaviour. If you have a custom excerpt set up in the post, it will show the full custom excerpt; this is how WP deals with excerpts.
I would recommend removing your custom excerpt, otherwise you need to add custom PHP code to alter the excerpt.
Let me know what you think.
-
tech@FHR
Hi Ying
Thank you for the quick reply. I didn’t know that about the excerpt behaviour.
I tried the php code below to limit the excerpt to 24 words but it had no impact on the GB Query Loop tempplate.
/**
* Filter the except length to 24 words.
*
* @param int $length Excerpt length.
* @return int (Maybe) modified excerpt length.
*/
function wpdocs_custom_excerpt_length( $length ) {
return 24;
}
add_filter( ‘excerpt_length’, ‘wpdocs_custom_excerpt_length’, 999 );Can you provide me with some code or point me in the right direction to resolve this issue, please?
Thank you!
-
Hi there,
Manual excerpts in WordPress will ignore that
excerpt_length
filter.You could try filtering the dynamic value itself like this:
add_filter( 'generateblocks_dynamic_content_output', function( $output, $attributes ) { if ( 'post-excerpt' !== $attributes['dynamicContentType'] ) { return $output; } $excerpt_length = $attributes['excerptLength'] ?? 20; return wp_trim_words( $output, $excerpt_length ); }, 10, 2 );
Hope this helps!
-
tech@FHR
Hi Tom
That worked perfectly, thank you!
Kind regards,
John -
You’re welcome 🙂
- You must be logged in to reply to this topic.