askdesign
8/28/2016 - 10:28 PM

How to add featured images before entry on single Posts in Genesis

August 28, 2016 by Sridhar Katakam

// Register a custom image size for featured image on single Posts
add_image_size( 'single-posts', 900, 540, true );
 
// Display featured image (if present) before entry on single Posts
add_action( 'genesis_before_entry', 'sk_featured_image' );
function sk_featured_image() {
    // if we are not on a single Post having a featured image, abort.
    if ( ! ( is_singular( 'post' ) && has_post_thumbnail() ) ) {
        return;
    }
 
    // get the URL of featured image
    $image = genesis_get_image( 'format=url&size=single-posts' );
 
    // get the alt text of featured image
    $thumb_id = get_post_thumbnail_id( get_the_ID() );
    $alt = get_post_meta( $thumb_id, '_wp_attachment_image_alt', true );
 
    // if no alt text is present for featured image, set it to Post title
    if ( '' == $alt ) {
        $alt = the_title_attribute( 'echo=0' );
    }
 
    // display the featured image
    printf( '<div class="single-post-image"><img src="%s" alt="%s" /></div>', esc_url( $image ), $alt );
}
.single-post-image img {
    vertical-align: top;
}