can the_excerpt function also get images?

Not sure if im going about this in the right way but, overall, ive been told to create, kind of like a news box on my home page and so, the “latest news” is going to be this clients posts.

now i know i can control the_excerpts(); content length etc but, i cant seem to find any way for it to also allow an image.

Read More

when i showed the client the “news box” he wants whatever image is associated with the post, to come through.

so i thought of "the_content();" but i dont think thats what i need(though im not sure which one will work best with what i need)

so the main questions here for me are

  1. Can i somhow get the image to come through with the excerpt, or
  2. If the excerpt function doesnt allow it, then what can i use?

Related posts

Leave a Reply

3 comments

  1. Ensure that your Theme supports Post Thumbnails, and that the client sets a “Featured Image” for each post. Then, combine the_excerpt() with the_post_thumbnail(), e.g. like so:

    <?php
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    
        ?>
        <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
            <div class="featured-image"><?php the_post_thumbnail(); ?></div>
            <div class="post-excerpt"><?php the_excerpt(); ?></div>
    
        </div>
        <?php
    
    endwhile; endif;
    ?>
    

    Then, just use CSS to style according to your needs.

  2. Place this in your functions.php file:

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'preserve_excerpt_format');
    function preserve_excerpt_format($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);
    
            $exceptions = '<p>,<a>,<em>,<strong>,<br><img>'; //PRESERVE THESE TAGS, ADD/REMOVE AS NEEDED
            $text = strip_tags($text, $exceptions);
    
            $maxCount = 55; //DEFAULT WP WORD COUNT, INCREASE AS NEEDED
            $excerpt_length = apply_filters('excerpt_length', $maxCount);
    
            $moreText = '.... <a class="blue" href="'.get_permalink($post->ID).'">Read More &gt;&gt;</a>'; //CUSTOM MORE TEXT, CHANGE AS NEEDED
            $excerpt_more = apply_filters('excerpt_more', $moreText);
    
            $words = preg_split("/[nrt ]+/", $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);
    }
    

    This should preserve any images that occur within the content’s HTML before the Word Limit is reached.