Get Image From Post w/ Multiple Fallbacks and Optional Timthumb
function get_the_image($height = “”, $width = “”) {
global $post, $posts;
// Check for a featured image
if (has_post_thumbnail()) {
$large_image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), ‘large’);
$post_thumbnail = $large_image_url[0];
// Grab an image attached to the post
} else {
$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)) {
foreach ($attachments as $attachment_id => $attachment) {
if (wp_get_attachment_image($attachment_id) != “”):
$post_thumbnail = wp_get_attachment_url($attachment_id);
endif;
}
// Grab the first image in the content
} else {
$first_img = ”;
ob_start();
ob_end_clean();
$output = preg_match_all(‘//i’, $post->post_content, $matches);
if (!empty($matches[1][0])) {
$post_thumbnail = $matches[1][0];
// Check the post meta for ‘image’
} else {
$post_thumbnail = get_post_meta($post->ID, ‘image’, $single = true);
//define default thumbnail, you can use full url here
if ($post_thumbnail == “”) {
$uploads = wp_upload_dir();
$post_thumbnail = AMP_PLUGIN_URL . “img/logo.png”;
}
}
}
}
if ($height != ” || $width != ”) {
$size = ”;
if ($height != ”) {
$size .= ‘&h=’ . $height;
}
if ($width != ”) {
$size .= ‘&w=’ . $width;
}
$post_thumbnail = AMP_PLUGIN_URL . ‘libs/timthumb.php?src=’ . $post_thumbnail . $size;
}
return $post_thumbnail;
}

