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.

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.

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?
share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
this works, thank you –  somdow May 2 '12 at 15:59
add comment

Have you tried using get_the_post_thumbnail()? You can read more about it on the Codex here. It pulls a thumbnail of the post's featured image.

share|improve this answer
add comment

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("/[\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);
}

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

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.