If/Else for acf_image

This is probably really simple, but I’ve spent a few hours on it and can’t figure it out, so…

I have a an acf_field image field called profile_photo that’s not a required field, however if there is an image stored in that field I’d like to display it, and if not display a placeholder.

I’ve tried a bunch of combinations including:
- Always results in True
- Always returns False

I’ve read the documentation and searched the forums, but I can’t figure it out. I’m sure someone else has solved this problem, so instead of continuing to spin my wheels, I thought I’d ask.

Thanks in advance!

The base L&L tags and attributes always work with the raw value of the field, so sometimes that can cause unexpected behaviors depending on how individual plugin developers make their plugins and fields work. For example, I noticed that <If field=image_field_name exists> doesn’t work, but that might just be because of some quirk with the values of ACF image fields.

In any case, for ACF, it’s always a good idea to use the ACF-specific syntax found in the docs. In this case since you want to check whether that field contains something and then (presumably) display it, I’d combine that with the if loop exists logic.

If you decide to go that route, you might end up with something like this:

<If loop exists acf_image=image_field_name>
  <Loop>
    <img src="{Field url}" />
  </Loop>
  <Else />
  <img src="https://www.sidekickinteractive.com/wp-content/uploads/2023/04/placeholder-3.png" />
</If>

Hope that helps!

1 Like

OK. It was simple and I finally figured it out. Here’s the solution that worked for me:

      <If field=profile_photo exists>

The integration with ACF was deeper than I expected, so I could specify the ACF field name of the field. Nice!

2 Likes