• Resolved maxgx

    (@maxgx)


    excerpt in posts grid is stripped of any formatting, resulting in a long line of simple text.

    i’m aware this is WP behavior, but would it be possible to filter it and allow some html tags (line break, for instance)?

    i think this has been mentioned before and welcomed as a good suggestion, but i’m not sure it has ever been implemented?

    thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author livemesh

    (@livemesh)

    Yes it is possible to filter the excerpt using the filter – ‘lae_posts_grid_entry_excerpt’ provided by the plugin. You can have a look at an example at https://gist.github.com/live-mesh/90a79048686651fa0bfbf0eb40493611 on using filters in general with our plugin.

    The premium version of the plugin has a checkbox to allow shortcodes and HTML on the excerpt. For the free version, since I haven’t tested it, can you pls check if the following code helps –

    add_filter('lae_posts_grid_entry_excerpt', 'mytheme_shortcode_excerpt', 10, 2);
    
    function mytheme_shortcode_excerpt($excerpt, $post_id) {
    
        $output = '<div class="entry-summary">';
    
        $excerpt_count = 120;
        
        $post = get_post($post_id);
    
        if (empty($post->post_excerpt))
            $excerpt = $post->post_content;
        else
            $excerpt = $post->post_excerpt;
    
        $output .= do_shortcode(force_balance_tags(html_entity_decode(wp_trim_words(htmlentities($excerpt), $excerpt_count, '…'))));
    
        $output .= '</div>';
    
        return $output;
    }
    Thread Starter maxgx

    (@maxgx)

    hi, thanks for that, i had already solved it using the solution provided here (valid for all excerpts throughout the site).

    i’ve read in force_balance_tags reference notes that “This function is not used for all WP pages due to bugs and performance issues”.

    not sure the code below is less “expensive”, but it works:

    /**
    * allow formatted text in excerpts
    **/
    function better_trim_excerpt($text)
    {
    $raw_excerpt = $text;
    if ( '' == $text ) {
    $text = get_the_content('');
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    
    // Removes any JavaScript in posts (between <script> and </script> tags)
    $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
    
    // Enable formatting in excerpts - Add HTML tags that you want to be parsed in excerpts, default is 55
    $text = strip_tags($text, '<strong><b><em><i><a><code><kbd><p><br><ul><ol><li><img>');
    
    // Set custom excerpt length - number of words to be shown in excerpts
    $excerpt_length = apply_filters('excerpt_length', 60);
    
    // Modify excerpt more string at the end from [...] to ...
    // $excerpt_more = apply_filters('excerpt_more', ' ' . '...');
    
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
    array_pop($words);
    $text = implode(' ', $words);
    
    // IMPORTANT! Prevents tags cutoff by excerpt (i.e. unclosed tags) from breaking formatting
    $text = force_balance_tags( $text );
    
    $text = $text . $excerpt_more;
    } else {
    $text = implode(' ', $words);
    }
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }
    
    // Remove the native excerpt function, and replace it with our improved function
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'better_trim_excerpt');
    Plugin Author livemesh

    (@livemesh)

    Glad you found something that works for you. Thanks for posting here the solution – sure benefits others who are looking for a solution to this.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Posts Grid excerpt formatting’ is closed to new replies.