The Supreme Thirst Quencher!
A picture is worth a thousand words and so a good wordpress theme has to have a lot of images. I usually use the function below which I can call from my theme pages to retrieve an image for an post.
function get_post_image( $iImageNumber = 0, $iSize = "thumbnail", $bPrint = false )
{
global $post;
global $wpdb;
$attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND
post_status = 'inherit' AND ( post_mime_type = 'image/gif' OR post_mime_type = 'image/jpeg'
OR post_mime_type = 'image/png' ) AND post_type='attachment' ORDER BY post_date ASC LIMIT 1");
$pics = wp_get_attachment_url($attachment_id, $size, false);
// If not image found as an attachment,
// then look inside the content for embeded images
if($pics == "")
{
$szPostContent = $post->post_content;
$szSearchPattern = '//';
preg_match( $szSearchPattern, $szPostContent, $images );
$pics = $images[1];
}
// If still not found then use a default image
if($pics == "") {
$loc = get_bloginfo('template_directory');
$pics = $loc . "/images/post-default-large.jpg";
}
if ( $bPrint == true && !empty($pics) ) echo "
"; else return $pics;
}