<?php
// ADD THIS IN THE functions.php FILE
function catch_that_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/uploads/default.jpg";
}
return $first_img;
}?>
---------------------------------------------------------------------
<?php
// ADD THIS IN THE DESIRED TEMPLATE FILE
$content = $post->post_content;
$searchimages = '~<img [^>]* />~';
/*Run preg_match_all to grab all the images and save the results in $pics*/
preg_match_all( $searchimages, $content, $pics );
// Check to see if we have at least 1 image
$iNumberOfPics = count($pics[0]);
if ( $iNumberOfPics > 0 ) { ?>
<!-- GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE -->
<img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
<?php }
?>