Catch the First Image
You can catch the your posts first image and use them for thumbnails. Also you can use default images for this. With this function you can use any image (first image on your posts also) for thumbnail. Lets Begin…
Here is the code;
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)){ //Varsayılan küçük resim
$first_img = "wp-content/themes/tema adı/images/default.png";
}
return $first_img;
}
This is the output;
catch_that_image()
This is the usage;
Usage with the img tag;
<img src="<?php echo catch_that_image() ?>" alt=""/>
Usage on site;
<span class="custom_image"> <a href="<?php the_permalink() ?>" rel="bookmark" title="Read: <?php the_title_attribute(); ?>"><img src="<?php echo catch_that_image() ?>" alt=""/> </a> </span>
And CSS;
.custom_image img{float:left; padding:3px; margin:7px 10px 15px 0; border: 2px solid #e6e6e6; height:90px; width:90px}
That’s All!

Thank you so much.
Can we grab images for Related Posts with this??
@Kishore Mylavarapu
Hi Kishore,
You can use Related Posts With Thumbnails or you can combine this post with Related Posts Without any plugin
Where do you put the code? Everywhere I put it, it creates errors on the functions.php editor???? I even had to reload my pluggable.php file. OYEEEE
nevermind figured it out, need to tell users to put at the top of the functions.php file just under the <?php tag.
John, sorry but i figured on all new wordpress themes have functions.php and code can insert to the end of file before "?>" The code worked for you? Have a nice day
Thanks!
Is there a way to modify this code to include styles in the Functions.php, as opposed to on the page. I’m trying to create thumbnails and I want the function to say something like, “if there’s an image post it with a width of 100 px, but if there’s not an image default image = null.”
I know it’s possible, but I can’t figure it out. Thanks!
Yes you can, I will write a post about this. Upgraded version of this code.
Thanks Serkan! You’re awesome! Will check it now.
You\’re welcome :)