Last active
December 18, 2015 12:59
-
-
Save halgatewood/5786736 to your computer and use it in GitHub Desktop.
Get Featured Image with Fall back for blank image.
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
// GET FEATURED IMAGE | |
function hg_get_featured_image( $id, $size = 'post-thumbnail', $allow_empty = false ) | |
{ | |
$rtn = false; | |
// TRY FEATURED IMAGE | |
$featured_image = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), $size ); | |
// IF NOT GET FIRST IMAGE ATTACHMENT FROM POST | |
if(!$featured_image) | |
{ | |
$attachments = get_children( array( 'post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'posts_per_page' => 1, 'orderby' => 'menu_order ASC, ID', 'order' => 'DESC') ); | |
if($attachments) | |
{ | |
$first_attachment = reset($attachments); | |
$featured_image = wp_get_attachment_image_src( $first_attachment->ID, $size ); | |
} | |
} | |
// IF FEATURED IMAGE GET THE SRC | |
if($featured_image) | |
{ | |
$rtn = $featured_image[0]; | |
} | |
// IF NOT RETURN DATA GET NO IMAGE | |
if($rtn == "" && !$allow_empty) | |
{ | |
$rtn = get_template_directory_uri() . "/images/no-image.jpg"; | |
} | |
return $rtn; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment