Site logo

[Resolved] Can the post content in the Dynamic Content block be limited in length?

Home Forums Support [Resolved] Can the post content in the Dynamic Content block be limited in length?

Home Forums Support Can the post content in the Dynamic Content block be limited in length?

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #2522315
    Arp

    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?

    #2522419
    Ying
    Staff
    Customer Support

    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/

    #2523433
    Arp

    Thanks Ying!

    #2523571
    Ying
    Staff
    Customer Support

    No Problem 🙂

    #2525245
    Arp

    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.

    #2525295
    Ying
    Staff
    Customer Support

    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' );
    
    
    #2526225
    Arp

    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?

    #2526577
    Ying
    Staff
    Customer Support

    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' );
Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.