Possible Duplicate:
Retrieve 1st image in post and set it as featured image, when post is saved/updated

I am trying to make this works and instead of giving me the url of the image, it is giving me "Array"

I have this code that gets the first image of the post

// Get URL of first image in a post
function catch_that_image() {
global $post, $posts;
$first_img = '';
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];

// no image found display default image instead
$first_img = "/images/default.jpg";

 $first_img = vt_resize( $first_img, '', 608, 250, true );

return $first_img;

and this code to display the image:

<img src="<?php echo catch_that_image() ?>" width="608" height="250" />

Could you please tell me what I am doing wrong and how to fix it please? Thank you.


Whoa, ok let's take a step back and use an actual WordPress function, that way your not doing anything weird like the above.

Use these:
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image (the images)
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src (array with atributes)
http://codex.wordpress.org/Function_Reference/the_post_thumbnail (featured image)

  • the vt_resize function is built in within the theme. – Gixty Apr 27 '12 at 1:48
  • I would delete any theme that used code like that. – Wyck Apr 27 '12 at 2:40
  • the problem is that my posts do not have thumbnails attached. So I need to scan for the first image in post and extract it. Then display it. The only thing I need to do now is to crop it so it can maintain its aspect ratio. – Gixty Apr 27 '12 at 3:31
  • If you have an image in a post, that means you uploaded it, thus you can use the above WordPress functions to get it, unless your post images are stored elsewhere. – Wyck Apr 27 '12 at 4:27
  • the images are stored in the media library. However, they are not attached to the post. Thus, I cannot set an image as featured. I am using wprobot for posting, so images are only uploaded to the media library but are not being attached to the post. – Gixty Apr 27 '12 at 4:51

Not the answer you're looking for? Browse other questions tagged or ask your own question.