Created
May 2, 2012 22:30
-
-
Save FernE97/2581030 to your computer and use it in GitHub Desktop.
PHP: WordPress Parent Featured Image
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
<?php | |
// recursively looks for featured images | |
function get_featured_recursive( $post ) { | |
if ( has_post_thumbnail( $post->ID ) ) { | |
return $post->ID; | |
} else if ( $post->post_parent != 0 ) { | |
return get_featured_recursive( get_post( $post->post_parent ) ); | |
} else { | |
return null; | |
} | |
} | |
// display on template | |
$featured_image_post = get_featured_recursive( $post ); | |
if ( $featured_image_post != null ) { | |
$featured_image_src = wp_get_attachment_image_src( get_post_thumbnail_id( $featured_image_post ), 'single-post-thumbnail' ); | |
} | |
?> | |
<img src="<?php echo $featured_image_src[0]; ?>" alt=""> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This no longer appears to work under Wordpress 3.5 - any ideas why?