I have modified a plug-in / function I saw in another post a while back.
What it does is it grabs the url of the first image of a post an outputs it to the catch_that_image() function.
I did some tinkering around and since I'm not very good at PHP, I've got myself into a block.
I use flickr to host most of my images in my blog, and I modified the code to grab the first image of the post, remove the ".jpg" extension, and then add a "_m.jpg" extension.
Adding a "_m" (or "_s", "_t") to a photo's filename in flickr will output a downsized thumbnail of the photo.
Here's 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];
// no image found display default image instead
if(empty($first_img)){
$first_img = "http://farm4.static.flickr.com/3414/3480595690_2a6071a1ce.jpg";
}
$first_img = preg_replace("/\.jpg$/i", "", $first_img);
return $first_img;
}
The problem is that instead of catching the first image of the post, it oftentimes catches the last image (someimes it also gets it right by catching the first image). Why does that happen? How to fix it?
I hope someone can help me.
Thanks