0

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()) { 

             the_post_thumbnail('thumbnail');

              } else

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

             the_post_thumbnail();

              } else {                    

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


              ?>             

Thanks very much.

1

2 Answers 2

0

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);
        $postOutput = str_replace(array('class="alignright"', 'class="alignleft"'), '', $postOutput);
        $postOutput = preg_replace('/title="([\w-_]+)"/',
                                   '',
                                   $postOutput);
        $image[$i] = $postOutput;
        $start=$imgEnd+1;
    }

    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));
        $j=0;
        $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;
    else:
        return get_post_category_image(NULL, $width);
    endif;
}
1
  • 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, 2011 at 12:59
0

I'm using this code

   <?php
    $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>
                    </div>


        <?php endforeach; endif; ?>

Your Answer

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

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