Skip to content

Instantly share code, notes, and snippets.

@geeac
Last active February 25, 2018 13:24
Show Gist options
  • Save geeac/ae8cc8c4da3ae9e66a7a935008ec25c7 to your computer and use it in GitHub Desktop.
Save geeac/ae8cc8c4da3ae9e66a7a935008ec25c7 to your computer and use it in GitHub Desktop.
Genesis - Add Featured Image on top of pages and posts
// Add Featured Image on top of pages as background image
add_action( 'genesis_after_header', 'featured_page_image', 11 );
function featured_page_image() {
if ( !is_singular('page') && !is_home() ) return;
if (is_home()){
// Get the ID of your posts page
$id = get_option('page_for_posts');
// Use the ID to get the post thumbnail or whatever
if ( has_post_thumbnail( $id ) ) {
//echo get_the_post_thumbnail( $id, 'page-featured', array( 'class' => 'aligncenter page-featured' ) );
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $id ), 'page-featured' );
}
}
else {
$post = get_post();
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'page-featured' );
}
}
if ( ! empty( $large_image_url[0] ) ) {
$featured_image = esc_url( $large_image_url[0] );
echo '<style>.page-featured {
background: url("'.$featured_image.'") no-repeat center;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
padding-top: 250px;
padding-bottom: 250px;
margin-top: 20px;
}</style>';
echo '<div class="page-featured"></div>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment