Change code for first occurrence only

This is probably a really dumb question but I’m quite new to php so am not sure what the answer is. I want to display a line of code ONLY for the occurrence of an image. At the moment it’s displaying it for every one except for the last. This is what I’ve got:

<?php if( $key<count( $fadeblock )-1 ) { ?> class="active" <?php } ?>

If you’re using foreach to loop over your array (something like foreach ($fadeblock as $key => $value) ) and your array index starts at zero, you could just do somethings like this:

<?php if ( $key === 0 ) { ?> class="active" <?php } ?>

Thank you that works a treat :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.