Home › Forums › Add-ons › Repeater Field › Repeater field option › Reply To: Repeater field option
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>';
} ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.