Support

Account

Home Forums Add-ons Repeater Field Repeater field option

Repeater field option

    • werther

    • September 16, 2013 at 6:24 pm

    Can you add an option (radio) in the repeater-field to return data in:

    – regular order (default)
    – random order (option)
    – inverse order (option)

    and an option (number) to select how many rows to return
    – returned items (number) // default = all
    // 3 = return first 3 items

    • Elliot

    • September 17, 2013 at 11:11 am

    Hi @werther

    This can be achieved with some simple PHP logic in your template.
    Adding these kinds of options in may result in bloating the streamlined aproach that ACF tries to portray.

    That said, I appreciate the feedback, but perhaps you could take a look at the docs for examples on modifying the repeater field?

    Thanks
    E

    • Nuro

    • September 17, 2013 at 6:32 pm

    This should work:

    – Create a Field Group
    – Add a Repeater
    – Add Sub Fields to Repeater
    – Add two Radio Button Fields to Field Group (Not as sub fields in the repeater!)

    I have these fields in my Field Group:
    “Repeater” => Repeater Field
    “IMG” => Repeater Sub Field, Field Type = Image, Return Value must be “Image ID”
    “IMG Title” => Repeater Sub Field, Field Type = Text
    “Row Sort” => Field Type = Radio Button, Default Value = “default”, Choices are set to:

    default : default
    random : random
    reverse : reverse

    “Row Limit” => Field Type = Radio Button, Option “Add ‘other’ choice to allow for custom values” is checked. Default Value = “3”, Choices are set to:

    1 : 1
    2 : 2
    3 : 3

    When the user creates a post, they can limit the number of Repeater rows to show and choose how the rows should be sorted (e.g. Random Order + Displaying 5 Rows).

    Tested and works, could probably clean code up or do some of it differently… this is what my tired mind came up with 🙂

    <?php
     
    $row_sort = get_field('row_sort' ); // radio button: random/reverse/default
    $row_limit = get_field('row_limit'); // radio button: 1/2/3/Custom
    
    $rows = get_field('repeater' );
    $sub_field = has_sub_field('repeater');
    
    $img = get_sub_field('img');
    $img_title = get_sub_field('img_title');
    
    if($rows) {
    
        echo '<h3>My List</h3>';
        echo '<ul>';
    
          for($i=0; $i < $row_limit; $i++) // Limits Rows based on field "Row Limit"
              {
    
                if($row_sort == "random") { // Random Order
    
                  /* Display Images Randomly (e.g. If 10 images have been added and Row Limit is set to 5, 5 Random Images will Display), $image[0] will pull the image links */
    
                  $rand_row = $rows[ array_rand( $rows ) ];
                  $rand_row_image = $rand_row['img' ];
                  $image = wp_get_attachment_image_src( $rand_row_image, 'full' );
    
                  echo '<li>';
                  echo $image[0]; // Return Value for Image must be set to "Image ID"
                  echo '</li>';
    
                } elseif($row_sort == "reverse") { // Reversed Order
    
                  /* Displays links to images in reversed order. If 10 images and Row Limit is set to 5, Images 10, 9, 8, 7, 6 will display */
    
                  $rowArray = get_field('repeater' );
                  $reversedArray = array_reverse( $rowArray );
                  
                      if($reversedArray) {
                      
                        $reversedArray = has_sub_field('repeater');
                        $img = get_sub_field('repeater');
                        $img_title = get_sub_field('repeater');
    
                        echo '<li><a href="' . $img . '">' . $img_title . '</a></li>';
    
                      }
    
                } else { // Default Order
    
                  /* Displays list of Image Titles like it normally would, but limited to Row Limit */
    
                    $default = get_field('repeater' );
                    $default = has_sub_field('repeater');
                  
                      if($default) { 
                          $img_title = get_sub_field('repeater');
                           
                          echo '<li>' . $img_title . '</li>';
                      }
    
                }
          
          }
                        
        echo '</ul>';
        
    } ?>
    • Elliot

    • September 18, 2013 at 11:42 am

    Hi @nuro

    Fantastic work!

    • Nuro

    • September 18, 2013 at 1:07 pm

    Thanks! 🙂

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.