I run into this situation frequently, when it is necessary to first check if any of the statements are true, then check for each individually. Just an example:
<?php
$a = get_field('a');
$b = get_field('b');
$c = get_field('c');
?>
<?php if( $a || $b || $c ): ?>
<div class="container">
<?php if( $a ): ?>
<span><?php echo $a; ?></span>
<?php endif; ?>
<?php if( $b ): ?>
<a href=""><?php echo $b; ?></a>
<?php endif; ?>
<?php if( $c ): ?>
<p><?php echo $c; ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
Although there is nothing wrong with this code, I always get the feeling after writing it that there should be a more efficient way. I'm wondering if anybody has come across one.
I thought about some kind of foreach statement, but the situation I normally come across is that the code within each if statement must be different, hence the use of span, a, and p elements.
Aucun commentaire:
Enregistrer un commentaire