Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/**
* Generate markup for the featured image above each page.
*
* If a featured image does not exist for the current page we will try and pull one from the parent page. If we still
* haven't found a featured image we will try and pull one from the 'page_on_front' (when using a static front-page).
* Otherwise we show nothing.
*/
function featured_image( $size = 'post-thumbnail' )
{
global $post;
if ( !is_page() ) {
return;
}
$the_post_thumbnail = get_the_post_thumbnail( $size );
// Get post_thumbnail from top most parent if we haven't found one yet.
if ( empty( $the_post_thumbnail ) ) {
$page_parents = get_post_ancestors( $post );
$topmost_parent = array_pop( $page_parents );
$the_post_thumbnail = get_the_post_thumbnail( $topmost_parent );
}
// Get post_thumbnail from "Front Page" template if we still don't find one.
if ( empty( $the_post_thumbnail ) ) {
$the_post_thumbnail = get_the_post_thumbnail( get_option( 'page_on_front' ), $size );
}
echo $the_post_thumbnail;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.