Last active
January 24, 2018 20:34
-
-
Save misfist/701d7fb7a19166ccc177c9541d88e07f to your computer and use it in GitHub Desktop.
WordPress - Find first image in post content
This file contains hidden or 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
<?php | |
function prefix_find_first_imamge( $post ) { | |
$content = $post->post_content; | |
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches ); | |
$first_img = $matches[1][0]; | |
if( empty( $first_img ) ) { | |
return false; | |
} | |
return $first_img; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment