This is the first post about the wordpress hack published on Bloganol about getting the first image from the post and displaying it.This is really useful wordpress hack and also requested by many users.Thanks to Wprecipes for publishing code for such important hack.
![]()
To implement this hack on your wordpress blog First of all copy the following block of code and paste it to your 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 = "/images/default.jpg";
}
return $first_img;
}
Now in second step Paste the following code where you would like to display the Image.This Tag simply call the function within the loop to display the first image from the post:
<img src="<?php echo catch_that_image() ?>" alt="" class="th" /> Consider to Subscribe to our RSS feed or Follow us at Twitter or Join our Facebook Fan Page.Enter your Email below to subscribe to our updates via Email.














{ 2 comments… read them below or add one }
can this work on blogger?
Since it is a wordpress hack so it doesn’t work on Blogger.There is different hack for blogger to help you.Visit here to learn