This snippet displays an ACF image field when an image is available. If the ACF image field is empty, it automatically falls back to the post’s Featured Image.
This is useful for WordPress listings, blog cards, custom post types, and archive templates where you want to give priority to a custom ACF image while keeping the Featured Image as a fallback.
The example below assumes that the ACF image field returns an image array.
<?php
// Get the ACF image field.
$acf_image = get_field( 'listing_page_image' );
if ( ! empty( $acf_image ) && ! empty( $acf_image['url'] ) ) {
echo '<img src="' . esc_url( $acf_image['url'] ) . '" alt="' . esc_attr( $acf_image['alt'] ) . '">';
} elseif ( has_post_thumbnail() ) {
the_post_thumbnail(
'full',
[
'loading' => 'lazy',
'decoding' => 'async',
]
);
}