Skip to content

Instantly share code, notes, and snippets.

@RuslanAsadov
Created December 9, 2019 07:56
Show Gist options
  • Save RuslanAsadov/f7d43ff342a294f8081c62b7d3799451 to your computer and use it in GitHub Desktop.
Save RuslanAsadov/f7d43ff342a294f8081c62b7d3799451 to your computer and use it in GitHub Desktop.
Get post image (WP)
<?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