0

Sometimes with Wordpress, I find myself writing long if/else chains to see if a field (a field I've created in advanced custom fields for example) exists, for example:

$imagen1            = get_field("imagen1");
$imagen2            = get_field("imagen2");
$imagen3            = get_field("imagen3");
$imagen4            = get_field("imagen4");
$imagen5            = get_field("imagen5");


        <?php if ( $imagen1 ) { ?>
            <div class="slider-item"> 
            <!-- <a href="#" class="slider-item-plus"> + </a> -->
              <img src="<?php echo $imagen1['url'] ?>" alt="slider photo" class="img-responsive">
           </div>
        <?php } ?>

        <?php if ( $imagen2 ) { ?>
            <div class="slider-item"> 
            <!-- <a href="#" class="slider-item-plus"> + </a> -->
              <img src="<?php echo $imagen2['url'] ?>" alt="slider photo" class="img-responsive">
           </div>
        <?php } ?>

        <?php if ( $imagen3 ) { ?>
            <div class="slider-item"> 
            <!-- <a href="#" class="slider-item-plus"> + </a> -->
              <img src="<?php echo $imagen3['url'] ?>" alt="slider photo" class="img-responsive">
           </div>
        <?php } ?>

        <?php if ( $imagen4 ) { ?>
            <div class="slider-item"> 
            <!-- <a href="#" class="slider-item-plus"> + </a> -->
              <img src="<?php echo $imagen4['url'] ?>" alt="slider photo" class="img-responsive">
           </div>
        <?php } ?>

        <?php if ( $imagen5 ) { ?>
            <div class="slider-item"> 
            <!-- <a href="#" class="slider-item-plus"> + </a> -->
              <img src="<?php echo $imagen5['url'] ?>" alt="slider photo" class="img-responsive">
           </div>
        <?php } ?>

I have seen this in a few webpages that I have inherited as well. It is not very effective as there is a limit in the amount of fields you can display since you have to add a new if chain everytime a new field is added. Can this be replaced with a loop? And if so, how would you write it?

2 Answers 2

1

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.

0

I've actually fixed this with this loop, in this case with a limit of 10 iterations.:

<?php 
    for( $iteration = 1; $iteration <= 10; $iteration++){ 
       if ( get_field("imagen".$iteration ) ){ ?>
            <div class="slider-item"> 
                <img src="<?php echo get_field("imagen".$iteration)['url'] ?>" alt="slider photo" class="img-responsive">
            </div>
        <?php } ?>
<?php } ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.