Created
December 25, 2014 18:00
-
-
Save matesnippets/f0652f15bc1c062524ad to your computer and use it in GitHub Desktop.
PHP, WP, Get the first image inserted into a post body
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get the first image inserted into a post body | |
*/ | |
function get_first_inserted_image() | |
{ | |
global $post, $posts; | |
preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); | |
$first_img = $matches [1] [0]; | |
// Spew it out as an imege | |
$first_img = '<img alt="Blog Post Image" src="' . $first_img . '">'; | |
return $first_img; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment