Last active
February 3, 2018 03:08
-
-
Save davidsword/0baa6d4ab341281bc273b7d7597ba772 to your computer and use it in GitHub Desktop.
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
<? | |
function return_a_damn_image() { | |
global $post; | |
// try to get featured image | |
$ftr = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID()), 'medium' ); | |
// try to get posts from post_content | |
preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/i',$post->post_content, $result); | |
// try to get attached media | |
$get_attached_images = get_attached_media('image',get_the_ID()); | |
foreach ($get_attached_images as $aattacheditem) { | |
$attached_img = wp_get_attachment_image_src($aattacheditem->ID,'medium'); | |
break; | |
} | |
// let's see: if featured image | |
if (is_single() && isset($ftr[2])) | |
$image = $ftr[0]; | |
// if an image from post_content | |
elseif (isset($result[1][0])) | |
$image = $result[1][0]; | |
// if an image from an attached image (gallery) | |
elseif (isset($attached_img[2])) | |
$image = $attached_img[0]; | |
// nothin! let's go default image | |
else | |
$image = get_bloginfo('stylesheet_directory')."/logo.png"; | |
return $image; // send image back | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment