0

I got this great excerpt function that keeps my paragraphs in tact. Works great btw.

 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(']]>', ']]>', $text);

    //Add the allowed HTML tags separated by a comma.
    $allowed_tags = '<p>,<a>,<em>,<strong>';  
    $text = strip_tags($text, $allowed_tags);

    //Change the excerpt word count.
    $excerpt_word_count = 105; 
    $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');

To show the first image with the excerpt, I use the old function

    function pietergoosen_get_first_image() {
    global $post, $posts;
$first_img = '';
if(preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
$first_img = $matches [1] [0];
} 
    return $first_img;
}

This also works great.

The question now is (I'm not a coder at all), is there a way and how to change the excerpt code I supplied to automatically display the first image without needing more functions. Any code will be highly appreciated

  • I don't understand the question. You have two functions which you say work correctly, so what are you asking? You want to know how to combine the functions? Why? To achieve what end? – s_ha_dum Feb 9 '14 at 19:42
  • Was just curious to know if you can somehow integrate these functions into one. Trying to clean up some of my codes in my child theme – Pieter Goosen Feb 9 '14 at 19:45
  • 2
    Separation of concerns. Getting the image and truncating text are two different operations, they should kept separated. – fuxia Feb 9 '14 at 19:47
  • 1
    Can you combine them? Probably. But why? It isn't possible to solve a problem without knowing the details of the problem. And as @toscho has noted, it may not even be a good idea. – s_ha_dum Feb 9 '14 at 19:51
0

For everyone that is looking for an excerpt that keeps images etc in tact without using the first image function, 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|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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