Skip to content

Instantly share code, notes, and snippets.

@sharkyak
Created November 18, 2015 14:35
Show Gist options
  • Select an option

  • Save sharkyak/beb21a437627a2a60a74 to your computer and use it in GitHub Desktop.

Select an option

Save sharkyak/beb21a437627a2a60a74 to your computer and use it in GitHub Desktop.
wordpress get image form post
function get_post_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)){
$img_dir = get_bloginfo('template_directory');
$first_img = $img_dir . '/images/pf1.jpg';
}
return $first_img;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment