Created
May 19, 2015 17:48
-
-
Save nicdford/e842ae674435e071c6e1 to your computer and use it in GitHub Desktop.
Featured Images Inside a Loop with image information
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 to page template | |
<?php if ( has_post_thumbnail() ) : ?> | |
<?php $attachment_meta = wp_get_attachment( get_post_thumbnail_id() ); ?> | |
<div class="featured-image"> | |
<img src="<?=$attachment_meta['src'];?>" alt="<?=$attachment_meta['alt'];?>" title="<?=$attachment_meta['title'];?>"> | |
</div> | |
<?php endif; ?> | |
// add below function to functions.php | |
function wp_get_attachment( $attachment_id ) { | |
$attachment = get_post( $attachment_id ); | |
return array( | |
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ), | |
'caption' => $attachment->post_excerpt, | |
'description' => $attachment->post_content, | |
'href' => get_permalink( $attachment->ID ), | |
'src' => $attachment->guid, | |
'title' => $attachment->post_title | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment