How to Use an ACF Image Field in WordPress

Quick Summary

  • Set an ACF Image field such as slide_one_image to return Image ID, so you can choose the output size in the template.
  • get_field returns the attachment ID, and wp_get_attachment_image_src gives the URL at index 0 in the size you request, such as full or medium.
  • The snippet leaves alt empty, so fill it in, or use wp_get_attachment_image to build the img tag with alt text and srcset.

An Advanced Custom Fields (ACF) Image field can return an image array, a URL or an attachment ID. This snippet uses the ID, which lets you choose the image size when you output it. Here the field is called slide_one_image, for example the image of a homepage slide.

Create an Image field with that name (or change the name in the code) and set its Return Format to Image ID. Then add the code to the template of the page or post that has the field.

PHP
<?php
$first_slide_image = get_field( "slide_one_image" );
$first_p_img = wp_get_attachment_image_src( $first_slide_image, "full" );
?>


<img src="<?php echo esc_url($first_p_img[0]); ?>" alt="">

How it works

  • get_field( "slide_one_image" ) returns the attachment ID of the chosen image.
  • wp_get_attachment_image_src() returns an array for that image in the requested size: the URL at index 0, followed by the width and height.
  • full loads the full-size image. Use thumbnail, medium, large or a custom size name to load a smaller file.
  • esc_url() makes the URL safe to print in the src attribute.

The alt attribute is empty, so fill it in with a short description. If the field can be left empty, check $first_slide_image before printing the tag; otherwise you get an img without a source. wp_get_attachment_image() is an alternative that builds the whole img tag for you, including the alt text saved in the media library and responsive srcset values.

For a theme where every image field, size and alt text is set up for you, see my custom WordPress development services.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More