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!


Related posts:

If you enjoyed this post, make sure you subscribe to my RSS Feed
Comments are closed.
Highslide for Wordpress Plugin