Skip to content

Instantly share code, notes, and snippets.

@mdemrs
Created December 8, 2017 16:32
Show Gist options
  • Save mdemrs/7012ce95e707b7151ffad564aa63477c to your computer and use it in GitHub Desktop.
Save mdemrs/7012ce95e707b7151ffad564aa63477c to your computer and use it in GitHub Desktop.
This example shows how to display a custom size of the selected image when using the image array return type. This return type allows us to easily access extra image data such as sizes, width, height and more.
<?php
$image = get_field('image');
if( !empty($image) ):
// vars
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
// thumbnail
$size = 'thumbnail';
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
if( $caption ): ?>
<div class="wp-caption">
<?php endif; ?>
<a href="<?php echo $url; ?>" title="<?php echo $title; ?>">
<img src="<?php echo $thumb; ?>" alt="<?php echo $alt; ?>" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />
</a>
<?php if( $caption ): ?>
<p class="wp-caption-text"><?php echo $caption; ?></p>
</div>
<?php endif; ?>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment