Last active
July 2, 2017 11:30
-
-
Save topleague/70b95c86b5ae64f4f34ebc362f6129d4 to your computer and use it in GitHub Desktop.
Display Featured Image with Caption in Genesis
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
// Code to Display Featured Image with Caption on top of the post | |
// @link https://sridharkatakam.com/add-featured-images-entry-single-posts-genesis/ | |
add_action( 'genesis_before_entry', 'featured_post_image', 8 ); | |
function featured_post_image() { | |
if ( ! is_singular( 'post' ) ) { | |
return; | |
} | |
$image = genesis_get_image( 'format=url&size=post-image' ); // get the URL of featured image. | |
$thumb_id = get_post_thumbnail_id( get_the_ID() ); // get the alt text of featured image. | |
$alt = get_post_meta( $thumb_id, '_wp_attachment_image_alt', true ); | |
$caption = get_post( $thumb_id )->post_excerpt; // get the caption of featured image. | |
$caption_html = $caption ? '<figcaption class="wp-caption-text">'. $caption . '</figcaption>' : ''; // Construct the caption HTML if caption is present for the featured image.. | |
printf( '<figure class="single-post-image wp-caption"><img src="%s" alt="%s" />%s</figure>', esc_url( $image ), $alt, $caption_html ); // display the featured image with caption (if present) beneath the image. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment