Home Forums Add-ons Repeater Field Repeater Toggle On & Off


Repeater Toggle On & Off

  • Hello, Im using a repeater named slides where Im generating a random home page on load every time. Id like to be able to keep all the slides(repeaters) in the backend but be able to remove them from the array that is getting randomized. Here is what i’m doing, how would I add a toggle (true/false) on the slide to pull it from the array on the component.

    Here is what I have at the moment.

    $slide = array_rand($component_fields['slides']);
      $slide = $component_fields['slides'][$slide]; 
  • I would add another field to my repeater, probably a true/false field to set display on/off. I would probably add some type of acf/validate_value filter to make sure that at least one row of the repeater is set to be shown because with them all set to not show the code I include would cause an infinite loop, but I would also build some kind of exit into the loop to prevent this as well.

    $slide = array_rand($component_fields['slides']);
    $slide = $component_fields['slides'][$slide];
    $count = 0;
    // use count to make sure we only to this X number of times to prevent infinite loop
    // and test display true/false field
    while ($count<10 && !$slide['true_false_field_name']) {
      // count is < 10 and this slide should not be shown
      // select a different slide
      $slide = array_rand($component_fields['slides']);
      $slide = $component_fields['slides'][$slide];
  • This worked perfect, thanks alot!!!

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

You must be logged in to reply to this topic.