0

Is it possible to include an image within the auto excerpt?

So in the editor you have image then some text and once live it includes the image and not just the text?

2 Answers 2

4

Yes, you can do that, but you'll need to tweak the <?php the_excerpt(); ?> function. This is a good article on how to do that:

http://aaronrussell.co.uk/legacy/improving-wordpress-the_excerpt/

To sum up the article in case it goes away, put this in your theme's functions.php file:

function improved_trim_excerpt($text) {
        global $post;
        if ( '' == $text ) {
                $text = get_the_content('');
                $text = apply_filters('the_content', $text);
                $text = str_replace('\]\]\>', ']]&gt;', $text);
                $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
                $text = strip_tags($text, '<img>');
                $excerpt_length = 80;
                $words = explode(' ', $text, $excerpt_length + 1);
                if (count($words)> $excerpt_length) {
                        array_pop($words);
                        array_push($words, '[...]');
                        $text = implode(' ', $words);
                }
        }
        return $text;
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'improved_trim_excerpt');

Note this line, this is where you're telling the function to strip all tags except <img> tags. You could add others if you'd like to include paragraph tags or whatever:

$text = strip_tags($text, '<img>');

Then call the_excerpt(); as usual in your theme files (index.php, single.php etc).

Best of luck!

0
1

A bit improvement .if you want to remove [..] with Read more and with a css class and Font awesome icon replace

array_push($words, '[...]');

with

array_push($words, '<a class="read-more" href="'. get_permalink( get_the_ID() ) . '">Read More<span><i class="icon-arrow-right"></i><span></a>');

Cheers!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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