Support

Account

Home Forums General Issues acf conditional logic with more than one image field

Solving

acf conditional logic with more than one image field

  • I want to write an if else statement that takes up to 4 image fields. If only one image is uploaded it will only show that in a col-12 grid. If two images are uploaded it will show each image in col-6 grid. If three images are uploaded it will show two images in col-6 and the third in col-12. If four images are uploaded it will show each in a col-6 grid. Please help.

  • What are the names of your fields? What are the return values of your fields?

    This is just a general idea because I don’t have the details.

    
    <?php 
    // get all for fields and add them to an array
    $images = array();
    $images[] = get_field('image_field_1');
    $images[] = get_field('image_field_2');
    $images[] = get_field('image_field_3');
    $images[] = get_field('image_field_4');
    
    $current_image = 0;
    foreach ($images as $image) {
      $class = 'col-12';
      if (($current_image == 0 || $current_image == 2) && isset($images[$current_images+1])) {
        $class 'col-6';
      }
      ?><div class="<?php echo $class; ?>">image here</div><?php 
      $current_image++;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.