Created
January 14, 2020 08:10
-
-
Save sudipto-me/40981251c62c13d4fbf2c1b0b6ebc5ea to your computer and use it in GitHub Desktop.
Get The first image of a post where there is no featured img
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
function get_first_image($post_id) | |
{ | |
$first_img = ''; | |
$content_post = get_post($post_id); | |
$content = $content_post->post_content; | |
$output = preg_match_all('/<img.+?src=[\'"]([^\'"]+)[\'"].*?>/i', $content, $matches); | |
$first_img = $matches[1][0]; | |
if ($first_img == '') { | |
$first_img = get_template_directory_uri() . '/assets/img/placeholder.jpg'; | |
} | |
return $first_img; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment