Skip to content

Instantly share code, notes, and snippets.

@adalbertopita
Created March 31, 2012 03:05
Show Gist options
  • Save adalbertopita/2258887 to your computer and use it in GitHub Desktop.
Save adalbertopita/2258887 to your computer and use it in GitHub Desktop.
Wordpress: Catch_that_image()
<?php 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 = "";
}
return $first_img;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment