Support

Account

Home Forums Add-ons Repeater Field If Sub_field equals…

Solving

If Sub_field equals…

  • I must be searching wrong. I’m sure this must have been covered. Basically, I want to display certain code only if a sub_field equals a certain value. In other words, I have a Repeater that is allowing the user to choose pages to display their content in a layout on a single page. All that is working fine. But I want to add some code if a specific page has been selected in that array. How would I do that?

  • Unfortunately, there isn’t any easy way to do that, which is probably why it’s not documented anywhere. There have been a few questions related to this here here.

    What you have to do is loop through the fields an look to see if the value exists and base the condition on whether or not is was found. The best way to do this depends on exactly what you’re trying to do as well as where and when you want to do it.

    Let’s say that you wanted to show something extra after the loop, possibly the easiest option.

    
    $found = false;
    if (have_rows('repeater')) {
      while(have_rows('repeater')) {
        the_row();
        if (get_sub_field('sub_field') == 'the value I'm looking for') {
          $found = true;
          // possibly collect more data from this row
        }
      }
    }
    if ($found) {
      // do the extra stuff
    }
    
  • This reply has been marked as private.
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘If Sub_field equals…’ is closed to new replies.