# ACF Image Field Shortcode

> Adds an [acf_image name="hero" size="large"] shortcode that outputs a responsive image from an ACF image field, whatever return format the field uses.

- Type: PHP
- Runs: Run everywhere
- Category: [Custom Post Types & Fields](https://wpide.com/library/category/post-types/)
- Tags: acf, images, shortcode
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/acf-image-field-shortcode/

## Code

```php
add_shortcode( 'acf_image', function ( $atts ) {
    $atts    = shortcode_atts( array( 'name' => '', 'size' => 'large', 'post_id' => 0, 'class' => '' ), $atts, 'acf_image' );
    $post_id = $atts['post_id'] ? absint( $atts['post_id'] ) : get_the_ID();

    if ( ! $post_id || ! function_exists( 'get_field' ) || ! is_post_publicly_viewable( $post_id ) ) {
        return '';
    }

    $value = get_field( sanitize_key( $atts['name'] ), $post_id, false ); // Raw value: the attachment ID.
    $id    = is_array( $value ) ? (int) ( $value['ID'] ?? 0 ) : absint( $value );

    if ( ! $id ) {
        return '';
    }

    return wp_get_attachment_image( $id, sanitize_key( $atts['size'] ), false, array( 'class' => sanitize_html_class( $atts['class'] ) ) );
} );
```

## Notes

The image's alt text comes from the Media Library. Add class="..." for styling.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "ACF Image Field Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
