Skip to content

Instantly share code, notes, and snippets.

@ajvillegas
Created May 10, 2017 23:25
Show Gist options
  • Save ajvillegas/8165ee358273978e3471ffcff8f1450e to your computer and use it in GitHub Desktop.
Save ajvillegas/8165ee358273978e3471ffcff8f1450e to your computer and use it in GitHub Desktop.
Illustrates how to display attachments using data extracted from wp_prepare_attachment_for_js().
<?php
/**
* Illustrates how to display a basic list of attachments when using data extracted
* from the wp_prepare_attachment_for_js() array.
*
* @author Alexis J. Villegas <alexis@ajvillegas.com>
* @link https://developer.wordpress.org/reference/functions/wp_prepare_attachment_for_js/
*/
// Attachment IDs
$images = array( '3508', '2519', '2461', '2801', );
// Display attachments
if ( $images ) { ?>
<div class="attachment-images"> <?php
foreach( $images as $image ) {
// Get attachment details
$attachment = wp_prepare_attachment_for_js( $image ); ?>
<div>
<a href="<?php echo $attachment['link']; ?>">
<img src="<?php echo $attachment['sizes']['medium']['url']; ?>" alt="<?php echo $attachment['alt']; ?>" />
</a>
<p><?php echo $attachment['caption']; ?></p>
</div> <?php
} ?>
</div> <?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment