Most WordPress users are using custom fields to display thumbs on their blog homepage. It is a good idea, but do you know that with a simple php function, you can grab the first image from the post, and display it.
to get you first image from the post just paste the below code in functions.php :
function first_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];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}once you have pasted the above code just , you can simply call the function within the loop to display the first image from the post:
<?php echo first_image(); ?>
