Refactoring long if/else php chains

There is no hard limit, but when you have many fields (I’m talking hundreds or thousands, not dozens), you might run into limits on execution time on your host.

In this case, I’d do something like this to turn it into a loop if you don’t need specific HTML.

foreach(range(1, 5, 1) as $imageNumber) {
    if ( $image = get_field("imagen" . $imageNumber) ) { ?>
        <div class="slider-item"> 
        <!-- <a href="#" class="slider-item-plus"> + </a> -->
            <img src="<?php echo $image['url'] ?>" alt="slider photo" class="img-responsive">
        </div>
    <?php }
}

You could still do specific HTML with this using switch, but I usually stop putting things into the same loop when I find them different enough that they need to get very different HTML.

tech