Created
May 7, 2016 19:04
-
-
Save michaelvandenberg/90cd45df51e201045db9be5cb45d493b 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
if ( ! function_exists( 'magnumopus_featured_image' ) ) : | |
/** | |
* Displays a featured image at the top of single posts and pages and pages with a portfolio or front page template. | |
* | |
* @since Magnum_Opus 1.0.0 | |
*/ | |
function magnumopus_featured_image() { | |
// Return if it's not home and there is no featured image. | |
if ( ! is_home() && ! has_post_thumbnail() ) { | |
return; | |
} | |
// Return if it's home and there is no featured image for the blog page. | |
if ( is_home() && ! has_post_thumbnail( get_option( 'page_for_posts' ) ) ) { | |
return; | |
} | |
// Get the ID of the blog page. | |
if ( get_option( 'page_for_posts' ) && is_home() ) { | |
$id = get_option( 'page_for_posts' ); | |
} | |
// Get the post ID. | |
else { | |
$id = get_the_ID(); | |
} | |
// Get the post thumbnail url. | |
$thumb_url = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'magnumopus-featured-img' ); | |
// Echo the background image. | |
echo 'style="background-image: url(' . esc_url( $thumb_url[0] ) . ');"'; | |
} | |
endif; // magnumopus_featured_image | |
// Don't forget to add add_image_size( 'magnumopus-featured-img', 1140, 600, true ); to functions.php. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment