Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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!

share|improve this answer
    
Thank you, i'll give it a go :) –  Jezthomp Mar 14 '12 at 20:42
add comment

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!!!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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