Support

Account

Home Forums Add-ons Repeater Field ACF Repeater max 6 items display inside table

Solving

ACF Repeater max 6 items display inside table

  • Hi I’m having a problem that i am curious if it is possible with repeater field. please see illustration below. Basically my maximum item should be 6 items so

    if i have input only 2 items the output should be:

    [1] [2]

    then if i have 3 only:

    [1] [3]
    [2]

    if i have 4 only:

    [1] [3]
    [2] [4]

    if 5 only:

    [1] [4]
    [2] [5]
    [3]

    and finally if i have 6 items which is the maximum, this should display:

    [1] [4]
    [2] [5]
    [3] [6]

    any help for them to display inside table with 2 columns? Please help me. thank you

  • You’re not going to be able to do this without some extremely complex coding.

    Is there a reason that you’re using a table?

    If a table is the only way you can do this then I would suggest putting each row of the repeater into html and then output that html in the order you want based on the number of rows.

    
    $row_html = array();
    while(have_rows('repeater')) {
      the_row();
      $index = get_row_index();
      $row_html[$index] = '<div>';
      $row_html[$index] .= get_sub_field('my_sub_field');
      $row_html[$index] .= '</div>';
    }
    

    You’ll still need some creative logic, but at least you won’t be trying to do it inside of the loop.

  • yes because i am creating a generator for newsletter. i hope there were possible solutions

  • hi I’m wondering why code won’t work when I put else if? I’m planning to do it manually since it will only have maximum of 6 items

    <?php $row_html = array();
     while(have_rows('repeater')) {
      the_row();
      $index = get_row_index();
      $row_html[$index] = '<div>';
      $row_html[$index] .= get_sub_field('repeater_sub');
      $row_html[$index] .= '</div>';
    } ?>
    <?php if (($row_html[$index])<=2) {
      echo 'test 2 items';
    } else if (($row_html[$index])<=3) {
      echo 'test 3 items';
    } else {
      echo 'Test';
    } ?> 

    please help. Thank you

  • Your if/else should look something like this

    
    if (count($row_html) <= 2) {
      echo 'test 2 items';
    } elseif (count($row_html) <= 3) {
      echo 'test 3 items';
    } else {
      echo 'test';
    }
    

    you could make that simpler to type and read by doing

    
    $count = count($row_html);
    if ($count <= 2) {
      echo 'test 2 items';
    } elseif ($count <= 3) {
      echo 'test 3 items';
    } else {
      echo 'test';
    }
    

    you can simplify it more by using a switch statement which is easier to read when you have a lot of conditions than an if/elseif statement

    
    switch (count($row_html) {
      case 1:
        echo '1 block';
        break;
      case 2:
        echo '2 blocks';
        break;
      case 3:
        echo '3 blocks';
        break;
      case 4:
        echo '4 blocks';
        break;
      case 5:
        echo '5 blocks';
        break;
      case 6:
        echo '6 blocks';
        break;
    }
    
  • Hi thank you. but I’m wondering, even if I have more than 2 items , the output is still 2? even though I set a condition if there are 3 items but still at your example it echoed 2. also switch case seem not working for count function. I don’t know but I really tried count function for repeater but doesn’t show correct output. it always shows “1” even if i have multiple rows. please help me. More important is that it counts the repeater rows then get each item that will put manually. thanks

  • Hi Finally figured it out. I realized that mistyped repeater sub name. Anyway since it’s only 6 items, for now this is my solution. Thank you for helping to start!

    <?php
     
     $row_html = array();
     while(have_rows('repeater')) {
      the_row();
      $index = get_row_index();
      $row_html[$index] = get_sub_field('repeatersub');
     }
     
     $count = count($row_html);
     
     if($count==1) {
      echo 'Im 1';
     }
     
     elseif($count==2){
      echo 'Im 2';
      echo $row_html[2];
      echo $row_html[1];
     }
     
     elseif($count==3){
      echo 'Im 3';
      echo $row_html[3];
      echo $row_html[1];
      echo $row_html[2];
     }
     
     elseif($count==4){
      echo 'Im 4';
      echo $row_html[4];
      echo $row_html[1];
      echo $row_html[2];
      echo $row_html[3];
     }
     
     elseif($count==5){
      echo 'Im 5';
      echo $row_html[2];
      echo $row_html[4];
      echo $row_html[3];
      echo $row_html[5];
      echo $row_html[1];
     }
     
     elseif($count==6){
      echo 'Im 6';
      echo $row_html[5];
      echo $row_html[6];
      echo $row_html[3];
      echo $row_html[2];
      echo $row_html[1];
      echo $row_html[4];
     }
     
     else {
      echo 'None';
     }
     
     ?>
Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘ACF Repeater max 6 items display inside table’ is closed to new replies.