Created
December 9, 2019 07:56
-
-
Save RuslanAsadov/f7d43ff342a294f8081c62b7d3799451 to your computer and use it in GitHub Desktop.
Get post image (WP)
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
<?php | |
function get_post_image($id, $placeholder_path) { | |
$post = get_post($id); | |
$content = $post->post_content; | |
$regex = '/src="([^"]*)"/'; | |
preg_match( $regex, $content, $matches ); | |
if (!empty($matches)) { | |
return $matches[1]; | |
} elseif (get_the_post_thumbnail_url($id)) { | |
return get_the_post_thumbnail_url($id); | |
} else { | |
return "$placeholder_path"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment