Support

Account

Home Forums General Issues Repeater field dumping into tables

Solved

Repeater field dumping into tables

  • Im trying to get my repeater field to dump into a table with every other row a different color like most tables are displayed. My code is spitting out two tds every other one. Here is my code, not sure where the issues is messing it up.

    <table>
    	<tbody>
    		<tr>
    			<th>Cookie Flavors</th>
    		</tr>
    			<?php if ( get_field( 'flavors' ) ): ?>
    			  <?php $index = 1; ?>
    			   <?php $totalNum = count( get_field('flavors') ); ?>
    			    <?php while ( has_sub_field( 'flavors' ) ): ?>
    			    <tr class="bg">
    							<td><?php the_sub_field('flavor'); ?></td>
    						</tr>
    			    <? if ($index % 2 == 0) : ?>
    			        <? if ($index < $totalNum) : ?>
    			          <tr>
    										<td><?php the_sub_field('flavor'); ?></td>
    									</tr>
    			        <? elseif ($index == $totalNum) : ?>
    			        <? endif; ?>
    			    <? endif; ?>
    			<?php $index++; ?>
    			<?php endwhile; ?>
    			<?php endif; ?>
    	</tbody>
    </table>
  • I wasn’t exactly sure what $totalNum is used for but this is an example that will add the class every other row, starting with the second

    
    <table>
      <tbody>
        <tr>
          <th>Cookie Flavors</th>
        </tr>
        <?php 
          if (get_field('flavors')) {
            $index = 1;
            while (has_sub_field('flavors')) {
              ?>
                <tr<?php 
                       if ($index % 2 != 0) {
                        ?> class="bg"<?php 
                      }
                    ?> class="bg">
                  <td><?php the_sub_field('flavor'); ?></td>
                </tr>
              <?php 
              $index++;
            } // end while
          } // end if
        ?>
      </tbody>
    </table>
    
  • This worked perfect and seems to be more efficient code as well. Thanks for the reply.

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Repeater field dumping into tables’ is closed to new replies.