Overview

The Image field allows an image to be uploaded and selected. This field makes us of the native WP media popup to handle the upload and selection process.

Screenshots

Settings

  • Return value: Customize the returned data. Choose from Object (array of data), URL (string), or ID (int)
  • Preview Size: Customize the image size is displayed when editing a post
  • Library: Limit the image selection to only those that have been uploaded to this post, or the entire library

Template usage

The image field will return either an array, a string or an int value depending on the return value set. Below are some examples of how you can use this data. Please note that all following examples use an image field called “image”, and if you are working with a sub field, remember to replace any get_field and the_field functions with the relative get_sub_field and the_sub_field functions.

Basic display (Object)

This example shows how to display the selected image when using the object return type. This return type allows us to easily access extra image data such as alt.

<?php 
 
$image = get_field('image');
 
if( !empty($image) ): ?>
 
	<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
 
<?php endif; ?>

 

Customized display (Object)

This example shows how to display a custom size of the selected image when using the object return type. This return type allows us to easily access extra image data such as sizes, width, height and more. To see the full data available, please debug the $image variable.

<?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; ?>

 

Basic display (ID)

This example shows how to display the selected image when using the ID return type. This return type allows us to efficiently load only the necessary image data.

<?php 
 
$image = get_field('image');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
 
if( $image ) {
 
	wp_get_attachment_image( $image, $size );
 
}
 
?>

Basic display (URL)

This example shows how to display the selected image when using the URL return type. This return type allows us to efficiently display a basic image but prevents us from loading any extra data about the image.

<?php if( get_field('image') ): ?>
 
	<img src="<?php the_field('image'); ?>" />
 
<?php endif; ?>