Home Forums General Issues Output image depending on field selection


Output image depending on field selection

  • I have a select field that lets the user choose between two images. I’m going about it in a very convoluted (conditional display of an image depending on the selection) way at the moment and it’s very buggy and was curious if there’s a better way to do it. Basically if a user selects choice A then one image file is output, B then a different image file is output instead of the value of the selection.

  • What is the difference between the 2 images?

    Is there some reason why there isn’t a single image field to upload they image they want shown?

    Can you supply more information about why it is set up this way?

  • The front end requires a choice between two different logo colors depending on the background that the user sets. White logo or red logo. Those are the only two options. Currently I have a select option where the user chooses red or white and then display both using elementor but show/hide one based on what the user chooses but it’s very clunky and doesn’t work well for repeater fields. So the option will always be one of two different images. The problem with letting the user upload or choose the image from the media library is that we’re trying to limit the possibility of human error.

  • I can’t really think of a better way to do this.

  • My initial thought was to do something like this where I replace the string of the value with the url of the image but wasn’t sure if that would do the trick

  • I’m not sure how you plan to use that. If you are talking about altering the image file using that filter then you would need to set the value to the post ID of the image you want returned. ACF only stores the ID for image fields.

  • Sorry what I meant was, keeping the field as is so its a select field that returns a string of “red” or “white” and then using something like the below to replace “red” with the url of the image

    function my_acf_load_value( $value, $post_id, $field ) {
    if( is_string($value) ) {
    $value = str_replace( ‘Old Company Name’, ‘New Company Name’, $value );
    return $value;

  • Again, that would depend on what field you are changing the value of.

    If you want to alter the value returned by the select field then you should use acf/format_value with a priority > 10. If you change the value of the select field before ACF formats it then it will return NULL if that value is not one of the selected options.

    On the other hand, if you want to select field to return a URL then just use the UEL as the value when creating the options of the select field

    {URL OF IMAGE 1} : Red
    {URL OF IMAGE 2} : White
  • That did it! I had no idea one could do that. Thanks so much for your patience.

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

You must be logged in to reply to this topic.