I’m using the modulo operator so that after a loop has ran 3 times, it is wrapped in a “row” div.
However, I would now like to add to the code slightly so that if the loop only runs twice, it changes the width of the columns to “large-6” instead of “large-4”.
Would this be possible or would it screw up the original modulo function?
Note – I’m using Advanced Custom Fields for this, which is where the “have_rows” function is coming from.
Current Code:
<?php if( have_rows('content_modules') ): ?>
<?php $i = 0; while( have_rows('content_modules') ): the_row(); ?>
<?php if($i % 3 == 0): ?>
<div class="row">
<?php endif; ?>
<div class="large-4 columns">
<?php if( !empty($link) ): ?>
<a href="<?php echo $link ?>"><img src="<?php echo $photo ?>" />
<h3><?php echo $title ?></h3></a>
<?php else: ?>
<img src="<?php echo $photo ?>" />
<h3><?php echo $title ?></h3>
<?php endif; ?>
<?php echo $content ?>
</div>
<?php if($i % 3 == 2): ?>
</div>
<?php endif; ?>
<?php $i++; endwhile; endif;?>
Ok, I assume you are using Advanced Custom Fields (ACF) or something else, because to the best of my knowledge (and Google’s)
have_rows()
is not WordPress function call.If it is the
have_rows()
from ACF, it returns a boolean, as the name implies. This makes your problem more difficult as you need to know BEFORE you start your loop how many rows there are.You could do a quick loop before the main loop, count the rows, and then decide which class you need for CSS. It’s not optimal but honestly, at some point you’ve got to count them to decide.
Once you count them, you can decide, set a variable, and then inside your existing loop, use your newly created variable.
Pretty sure you would have to reset the loop with a
wp_reset_query()
between your loops. I could be wrong about that though.HTH,
=C=