Home Forums General Issues Fetching default image option


Fetching default image option

  • I implemented this code so that I can get the default image option in my image field.

    The issue is that, say I’m on my home page where I am showcasing all the posts, in this case get_field( 'image_field' ) will fetch the image_field from the first post, and I’d like it to fallback to the default value.

    How do I fetch that default image in this case?

  • I’ve been looking into this. As with most things in ACF, when a field has never been set you need to use the field key to get the field value. If you use the field key like


    then ACF will return the default value when a field has never been set.

  • Short of this you’ll need to resort to

    $image = get_field('image');
    if (!$image) {
      // code here to set $image to default value
      // you can probably use get_field_object($field_key)
      // end get the setting for default_value

    but it would probably be much easier to just use the key in get_field in the first place.

  • I will try it with the second option, I’m not sure that the first one is suitable for development (don’t know if the field numerical id will change). Thanks for the suggestion.

  • Not the ID (as in post ID that can change ) but the field key. The field key will not change unless you export to PHP and change it yourself or you delete the field and recreate it. The field key is a constant that can be depended on.

  • Oh, I get it, thanks!

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Fetching default image option’ is closed to new replies.