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?