WordPress: Get the first image from the post and display it

This function return the first image of the post (even if it dont be on the content) the ’size’ parameter let you define the size of the image (thumbnail or medium) and the ‘add’ parameter add further parameters to the image tag, like class or width and height.

function the_thumb($size = “medium”, $add = “”) {
       global $wpdb, $post;
       $thumb = $wpdb->get_row(”SELECT ID, post_title FROM {$wpdb->posts} WHERE post_parent = {$post->ID} AND post_mime_type LIKE ‘image%’ ORDER BY menu_order”);
       if(!empty($thumb)) {
               $image = image_downsize($thumb->ID, $size);
               print<img src={$image[0]}’ alt={$thumb->post_title}{$add} >;
       }
}

Ex. the_thumb(’medium’, ‘class=”alignleft” width=”200″ height=”175″‘);