WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm working on a conditional statement (in a loop) that looks for a featured image then grabs a default image if there is none, which I have figured out. But, I also wanted a way to pull the first image (or any image) attachment before going the route of the default image. Having trouble finding the right way to check for an attachment image, then display it.

I have added a specific thumbnail size, "featured-thumbs" in the functions.php file of my theme. It would be great if I could utilize this when displaying the attachment image.

Here's what I have so far:

<?php $count=0; if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

                        <div class="span-9 subfeature append-bottom <?php if(++$count%2==0) echo 'last'?>">                                           

              <?php $thumb = get_the_post_thumbnail(); //Hack for Sitewide Tags Thumbnail process: assigns variable for use in conditional statement below ?>

              <?php if (has_post_thumbnail()) { 


              } else

              if ( $thumb ) { //if not checks to see if there's a Sitewide Tags created thumbnail and uses that if there is


              } else {                    

               echo '<img src="'.get_bloginfo("stylesheet_directory").'/images/maintenance/fallback-thumb.jpg" >';


Thanks very much.

share|improve this question
Relevant article from Ottopress.com – jw60660 Sep 30 '11 at 18:29
up vote 0 down vote accepted

I'm using this code below, it's quite a bit, but it basically loops through post thumbnail, attached image, image in content and then to an image per category (which is a fixed set on my site, hence the naming scheme of the images in get_post_category_image. The main usage is through the last function, simply do echo get_post_image():

function get_post_content_thumbnail($num, $width='', $height='') {
    $content = get_the_content();
    $count = substr_count($content, '<img');
    $start = 0;
    for($i=1;$i<=$count;$i++) {
        $imgBeg = strpos($content, '<img', $start);
        $post = substr($content, $imgBeg);
        $imgEnd = strpos($post, '>');
        $postOutput = substr($post, 0, $imgEnd+1);
        $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"/',
        $postOutput = str_replace(array('class="alignright"', 'class="alignleft"'), '', $postOutput);
        $postOutput = preg_replace('/title="([\w-_]+)"/',
        $image[$i] = $postOutput;

    if(isset($image) && stristr($image[$num],'<img')) {
        return str_replace('<img ', '<img ' . image_hwstring($width, $height), $image[$num]);

function get_post_attachment_thumbnail($width='', $height='') {
    $files = get_children('post_parent='.get_the_ID().'&post_type=attachment&post_mime_type=image');
    if($files) {
        $keys = array_reverse(array_keys($files));
        $attachment_id = $keys[$j];
        $image = image_downsize($attachment_id, 'thumbnail');
        $image[0] = str_replace('http://yoast.com', 'http://cdn.yoast.com', $image[0]);
        return '<img src="' . $image[0] . '" alt="" ' . image_hwstring($width, $height) . '/>';

function get_post_category_image($p_id=null, $width='', $height=''){
    global $post_id, $top_category;
    if (!$p_id)
        $p_id = $post_id;

    if ( isset($top_category) )
        return $top_category;

    $post_categories = get_the_category( $p_id );

    $slug = count($post_categories) ? $post_categories[0]->slug : '';

    if ($slug && file_exists(TEMPLATEPATH . '/img/post-img/'.$slug.'.png')){
        return '<img ' . image_hwstring($width, $height). ' src="' . get_bloginfo('template_url') . '/img/post-img/'.$slug.'.png" alt="'.$post_categories[0]->cat_name.'"/>';
    } else {
        return '<img ' . image_hwstring($width, $height). ' src="' . get_bloginfo('template_url') . '/img/post-img/default.jpg" alt=""/>';

function get_post_image($string_size='post-thumbnail', $width=0){
    if ( has_post_thumbnail() ) :
        return get_the_post_thumbnail(NULL, $string_size, array('title'=>''));
    elseif( $attached_image = get_post_attachment_thumbnail($width)):
        return $attached_image;
    elseif( $post_content_thumbnail = get_post_content_thumbnail(1, $width)):
        return $post_content_thumbnail;
        return get_post_category_image(NULL, $width);
share|improve this answer
Joost, thanks, that's some elegant code. I'm a PHP beginner. I added the code I have so far into my original post. I'm not sure exactly how to use just the part I need - determining if there is an image attachment, then displaying it using a set size. Thanks very much. – jw60660 Sep 27 '11 at 12:59

I'm using this code

    $args = array( 
        'post_type' => 'attachment', 
        'numberposts' => -1, 
        'post_mime_type' => 'image',
        'post_status' => null, 
        'post_parent' => get_the_ID(), 
        'orderby' => 'menu_order', 
        'order' => 'ASC',
    $attachments = get_posts($args);
    if ($attachments):
            foreach($attachments as $key => $image):

                $attached_image = wp_get_attachment_metadata ( $image->ID);
                $upload_dir = wp_upload_dir();


                    <div class="thumb <?php echo ($attached_image['sizes']['thumbnail']['width'] == "200") ? "hor" : "ver"; ?>">
                        <a href="<?php echo $upload_dir['baseurl'] . "/" . $attached_image['file']; ?>" rel="colorbox"><img src="<?php echo $upload_dir['baseurl'] . "/" . $attached_image['sizes']['thumbnail']['file']; ?>" alt="" width="<?php echo $attached_image['sizes']['thumbnail']['width'] * .6; ?>" height="<?php echo $attached_image['sizes']['thumbnail']['height'] * .6; ?>" /></a>

        <?php endforeach; endif; ?>
share|improve this answer

Your Answer


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.