Skip to content

Instantly share code, notes, and snippets.

@nicdford
Created May 19, 2015 17:48
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 nicdford/e842ae674435e071c6e1 to your computer and use it in GitHub Desktop.
Save nicdford/e842ae674435e071c6e1 to your computer and use it in GitHub Desktop.
Featured Images Inside a Loop with image information
// 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