Assuming you’re following this tutorial:
http://wp.tutsplus.com/tutorials/automagic-post-thumbnails-image-management/
Replace the echo statement at the end of get_attachment_picture with a return, then change your image code.
Here is your new image code:
// get the URL of the image
$src = get_attachment_picture();
// if the url is not empty, display the image
if(!empty($src)){
?>
<img src="https://wordpress.stackexchange.com/questions/48896/<?php echo $src; ?>" />
<?php
} else {
// the url was empty, do not display the image
}
Here’s the modified function:
function get_attachment_picture(){
global $post, $posts;
$related_thumbnail = get_post_meta($post->ID, 'image', true); //read post meta for image url
if($related_thumbnail == ""):
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1,
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ASC'
) );
if(!empty($attachments)): //check if there an attachment or not
foreach ( $attachments as $attachment_id => $attachment ) {
if(wp_get_attachment_image($attachment_id) != ""):
$related_thumbnail = wp_get_attachment_url( $attachment_id );
endif;
}
else: // if no attachment
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(!empty($first_img)):
$related_thumbnail = $first_img;
else:
$related_thumbnail = "";
endif;
endif;
endif;
return $related_thumbnail;
}