Display the First image from a Posts


function catch_that_image() {
  global $post, $posts;
  $first_img = get_post_meta($post->ID, 'thumb', true);
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'
"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  if($output == 1 && empty($first_img)){ //Defines a default image
  $first_img = $matches [1][0];
  }
  else if(empty($first_img) && $output == 0) { $first_img = "no_image_url";
  }
  return $first_img;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s