Support

Account

Home Forums Add-ons Repeater Field Change class of element depending on amount of Repeater Field items Reply To: Change class of element depending on amount of Repeater Field items

  • Hi,

    I’m not sure how to implement this code you gave me.

    I’ve tried this:

      <?php
          $row_count = count(get_field('highlight'));
          $size = 0;
          if ($row_count) {
            $size = (int)12/$row_count;
          }
          if ($size < 1) {
            //force minimum of col-lg-6
            $size = 6;
          }
          if ($size < 2) {
            //force minimum of col-lg-4
            $size = 4;
          }
          if ($size < 3) {
            //force minimum of col-lg-3
            $size = 3;
          }
          // your repeater loop here
          if( have_rows('highlight') ): while ( have_rows('highlight') ) : the_row();?>
          <a href="<?php the_sub_field('highlight_link');?>">
          <div class="<?php echo 'col-lg-'.$size; ?> <?php echo 'col-md-'.$size; ?> <?php echo 'col-sm-'.$size; ?> <?php echo 'col-xs-'.$size; ?> highlight" style="background-image:url('<?php the_sub_field('highlight_image');?>');">
            <div class="highlight_title">
              <div class="highlight_title_inner">
                <?php the_sub_field('highlight_title');?>
              </div>
            </div>
          </div>
          </a>
        <?php endwhile; else : endif;?>

    Yet it doesn’t output the classes I want.

    How can I make sure that when I have:
    1 item, the size is 12.
    2 items, the size is 6.
    3 items, the size is 4.
    4 items, the size is 3.

    Thanks in advance.