Skip to content

Instantly share code, notes, and snippets.

@manhleo93
Created July 23, 2017 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manhleo93/713d31523a72931882ecbeb1cb7da70e to your computer and use it in GitHub Desktop.
Save manhleo93/713d31523a72931882ecbeb1cb7da70e to your computer and use it in GitHub Desktop.
// Code to Display Featured Image with Caption on top of the post
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