I have a problem with an acf function for displaying an image by array.

I can get my image to work well when I upload it to the field associated with it. On the other hand, when I don't put an image, because the field is not mandatory, I get this error warning: trying to access array offset on value of type bool

My image field is in a field group, in a page option and I'm in php 8.0

My code looks like this

$image = get_field('my_group','option');
$image_name = $image ['my_img'];
if ($image):?>
    <img src="<?php echo esc_url ($image_name['url']); ?>" alt="<?php echo esc_attr($image_name['alt']); ?>" />
<?php endif;?>
  • 1
    Are you sure that $image and $image_name are arrays? The warning you're getting indicates that you're trying to treat a boolean value (ie, either true or false) as an array.
    – Pat J
    Oct 9, 2023 at 21:38
  • Maybe I explained it wrong. In any case my goal is to use the example from the acf doc for "display image (array)" advancedcustomfields.com/resources/image
    – Thica
    Oct 10, 2023 at 7:03
  • You'll have to check with ACF support about this. I do suggest you check to see if your get_field() call is returning an array and not, say, false.
    – Pat J
    Oct 10, 2023 at 14:21


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.