Return to Snippet

Revision: 26974
at May 17, 2010 20:01 by creativelifeform


Initial Code
//grabs the first image from a post
 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))
  { //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

Initial URL


Initial Description
catches the first image of a post so you can display it however you like within the loop

Initial Title
Catch that image

Initial Tags
wordpress

Initial Language
PHP