Archived topic
Can the post content in the Dynamic Content block be limited in length?
7 replies · Started by Arp on February 5, 2023
The problem with using the excerpt is that it doesn't have any html for embeds - and I'm trying to setup an archive with post formats (similar to Tumblr). I'm guessing there must be a snippet to limit the length of the post content in a Dynamic Content block?
Hi there,
This is not controlled by the theme, you can use a plugin for that:
https://www.wpbeginner.com/plugins/how-to-customize-wordpress-excerpts-no-coding-required/
Thanks Ying!
No Problem :)
I finally got around to reading the page and while it talks about customizing excerpts, it says nothing about enabling html in excerpts.
I need to either A) enable HTML in excerpts or B) limit the length of the content produced by the Dynamic Content block.
Wrote this code, but not sure if it's going to work well with GP dynamic content block, give it a try and let me know :)
I've set the length to 20 words, feel free to change the 20 to your desired value.
function limit_content_length_with_html( $content ) {
if( is_archive() || is_home() ) {
return force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( $content ), 20, '...' ) ) );
}
return $content;
}
add_filter( 'the_content', 'limit_content_length_with_html' );
That worked! The only thing missing is the Read More button. Is that easy to add?
I also spent the last 2 hours trying to customize the function a bit to strip the tags for a particular category (or show an excerpt for that category), to no avail. Any suggestions for that?
I modified the snippet a bit, just need to replace the 1 of is_category('1') with your category ID which can be found in the URL of the category editor.
function limit_content_length_with_html( $content ) {
if( (is_archive() && ! is_category('1'))|| is_home() ) {
$content = force_balance_tags( html_entity_decode( wp_trim_words( htmlentities( $content ), 20, '...' ) ) );
$read_more_link = '<a href="' . get_permalink() . '">Read More</a>';
$content .= $read_more_link;
}
return $content;
}
add_filter( 'the_content', 'limit_content_length_with_html' );