Last active
February 25, 2018 13:24
-
-
Save geeac/ae8cc8c4da3ae9e66a7a935008ec25c7 to your computer and use it in GitHub Desktop.
Genesis - Add Featured Image on top of pages and posts
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
// 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>'; | |
} | |
} |
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
//* Add Featured Image on top of pages and posts | |
add_action( 'genesis_after_header', 'featured_page_image' ); | |
function featured_page_image() { | |
if ( is_singular(array('page', 'post')) && has_post_thumbnail() ) { | |
//the_post_thumbnail('large'); //you can use medium, large or a custom size | |
$image_args = array( | |
'size' => 'page-featured', | |
'attr' => array( | |
'class' => 'aligncenter page-featured', | |
), | |
); | |
genesis_image( $image_args ); | |
} | |
else 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' ) ); | |
} | |
} | |
else if( is_page ( array( 'cart', 'checkout' ) ) || is_shop() || 'product' == get_post_type() ) { | |
// Get the ID of your shop page | |
$id = get_option('woocommerce_shop_page_id'); | |
// 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' ) ); | |
} | |
} | |
} |
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
//* 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')) return; | |
$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