Last active
November 1, 2018 08:11
-
-
Save ABooooo/e1ff5b027fd996a64978 to your computer and use it in GitHub Desktop.
get image from post
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
Put this in your functions.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 = "/images/default.jpg"; | |
} | |
return $first_img; | |
} | |
Then use in your template getTheFirstImage() function where you want to print the image | |
<?php echo catch_that_image() ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment