Home Forums ACF PRO Different layout if 2 or more rows?


Different layout if 2 or more rows?

  • I’m trying to have a different layout if there are more than 1 rows in a repeater/flexible content.

    So if there is only 1 item/row it shows x if there are more than 1 so 2 or more it shows y.

    Tried the below but its adding the style to the first row regardless, rather than counting the rows and spitting out the different content.

    <?php if ( have_rows( 'row_content_boxes' ) ):?>
    <?php while ( have_rows( 'row_content_boxes' ) ) : the_row(); 
    							 										$is_single_post = get_row_index();
    <?php if (  $is_single_post  >= 2  ): //If has 2 rows?>
    									 									If only has 2 or more item in row show this					 									
    								 									<?php else: ?>
    								 									If only has 1 item in row show this				 									
    								 									<?php endif; ?>	
    <?php endwhile; ?>
    						 								<?php else: ?>
    							 									<?php // no rows found ?>
    							 								<?php endif; ?>
  • Not exactly sure what you’re trying to do to be honest.

    get_row_index() will only give you the current index, not the number of rows. This is good if you want to alter something based on the current row, but not if you are trying to change something based on the number of rows.

    if you want to get the number of rows

    $rows = count(get_field('row_content_boxes'));
  • I’ve a repeater/flexible content and it has 2 rows in it, i want it to show a certain div however, if its only got 1 i want it to show another.

    Whatever i try it always adds it to the first row even if there is 2, when i only want it to add to the first one if thats the only one, not the first one of 2.

    Does that explain better?

      $row_count = count(get_field('row_content_boxes'));
      if (have_rows('row_content_boxes')) {
        if ($row_count == 1) {
          // only one row
        } else {
          // more than one row
        while (have_rows('row_content_boxes')) {
          // code for each row here
  • Brilliant, thank you John.

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

The topic ‘Different layout if 2 or more rows?’ is closed to new replies.