0

My excerpts are shown using this function

 <?php   function get_the_content_limit_custom_allowedtags() {
// Add custom tags to this string
    return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>'; 
}

function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
if ( '' == $text ) {
    $text = get_the_content('');

    $text = strip_shortcodes( $text );

    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);

    //Add the allowed HTML tags separated by a comma.
    $text = strip_tags($text, get_the_content_limit_custom_allowedtags());

    //Change the excerpt word count.
    if( get_post_type() == 'information' ) {
        $excerpt_word_count = 55;
    } else {
    $excerpt_word_count = 85; 
    }
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 

    //Change the excerpt ending.
    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&hellip;' . __( 'Read more about this article <span class="meta-nav">&rarr;</span>', 'pietergoosen' ) . '</a>'; 
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);

    $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);
        $text = $text . $excerpt_more;
    } else {
        $text = implode(' ', $words);
    }
}
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); ?>

The problem I'm facing is that the html tags lead to a loss of words when using the_excerpt(). If I have a image at the beginning of my post, that post is about 20 words shorter than a post with no image attached to it. I checked out a couple of solutions, and all come down to the preg_slit() function. I replace the one in my code with a couple I found, but they mess up the excerpt. I used '/(<a.*?a>)|\n|\r|\t|\s/', '|\[(.+?)\](.+?\[/\\1\])?|s'. Any ideas to stop the_excerpt() from counting the html tags as words

1

For everybody that is looking for a great excerpt that keep html tags in tact and want the excerpt not to cut off mid sentence and have a true word count, here is the code

 function pietergoosen_custom_wp_trim_excerpt($text) {
global $post;
$raw_excerpt = $text;
    if ( '' == $text ) {

        $text = get_the_content('');
        $text = strip_shortcodes( $text );
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]&gt;', $text);

            //Add the allowed HTML tags separated by a comma.
            $allowed_tags = array(
            'head', 'title', 'base', 'link', 'meta', 'style', 'script', 'noscript', 'body', 'section', 'nav',
            'article', 'aside', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'header', 'footer', 'address', 'main', 'p', 'hr',
            'pre', 'blockquote', 'ol', 'ul', 'li', 'dl', 'dt', 'dd', 'figure', 'figcaption', 'div', 'a', 'em', 'strong',
            'small', 's', 'cite', 'q', 'dfn', 'abbr', 'data', 'time', 'code', 'var', 'samp', 'kbd', 'sub', 'sup', 'i', 'b',
            'u', 'mark', 'ruby', 'rt', 'rp', 'bdi', 'bdo', 'span', 'br', 'wbr', 'ins', 'del', 'img', 'iframe', 'embed',
            'object', 'param', 'video' ,'audio', 'source', 'track', 'canvas', 'map', 'area', 'svg', 'math', 'table',
            'caption', 'colgroup', 'col', 'tbody', 'thead', 'tfoot', 'tr', 'td', 'th', 'form', 'fieldset', 'legend', 'label',
            'input', 'button', 'select', 'datalist', 'optgroup', 'option', 'textarea', 'keygen', 'output', 'progress', 'meter',
            'details', 'summary', 'menuitem', 'menu'
            );

            $tag_string = '<' . implode('><', $allowed_tags) . '>';

        $text = strip_tags($text, $tag_string);

        //Set the excerpt word count and only break after sentence is complete.
            $excerpt_word_count = 75;
            $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 
            $tokens = array();
            $excerptOutput = '';
            $count = 0;

            // Divide the string into tokens; HTML tags, or words, followed by any whitespace
            preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $text, $tokens);

            foreach ($tokens[0] as $token) { 

                if ($count >= $excerpt_word_count && preg_match('/[\?\.\!]\s*$/uS', $token)) { 
                // Limit reached, continue until ? . or ! occur at the end
                    $excerptOutput .= trim($token);
                    break;
                }

                // Add words to complete sentence
                $count++;

                // Append what's left of the token
                $excerptOutput .= $token;
            }

        $text = trim(force_balance_tags($excerptOutput));

            $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'pietergoosen' ), get_the_title()) . '</a>'; 
            $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

            $pos = strrpos($text, '</');

        // Add 'Read more' text inside last HTML tag
        $text = substr_replace($text, $excerpt_end, $pos, 0);

    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); 
| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.