Home Forums Add-ons Repeater Field Only display Repeater row with certain Select value


Only display Repeater row with certain Select value

  • Hi all,

    I’ll try and keep this brief and understandable.

    I have a Repeater (testimonial_repeater).

    Inside that Repeater I have a Text Area (testimonial_text) and a Select (testimonial_type).

    The Select fields has 3 options:
    testimonial_type_workshops : Workshops
    testimonial_type_tours : Tours
    testimonial_type_general : General

    I have a Testimonial page, that shows all the entries. All good there.

    What I’m also trying to do is, on my Workshops page, only show the Repeater rows, where Workshops has been chosen.

    It all seemed quite logical, but I can’t for the life of me get it right.

    Any and all help will be greatly appreciated.

    if (have_rows('testimonial_repeater')) {
      while (have_rows('testimonial_repeater')) {
        if (get_sub_field('testimonial_type') != 'testimonial_type_workshops') {
          // not what we want, skip the rest of the loop
        // this code will only be run if right type
        .... do something ...
      } // end while have_rows
    } // end if have_rows
  • Hi John, thanks so much for your reply. I tried you example and many variations of it, but I’m not getting any output onto the page. Not even wrong stuff.

    Here is the stock standard code I’ve used for the Testimonial page, where all Repeater rows are displayed.

    <div class="testimonials">
    	<?php if( have_rows('testimonial_repeater') ): ?>
    		<?php while( have_rows('testimonial_repeater') ): the_row(); 
    			$text = get_sub_field('testimonial_text');
    			$type = get_sub_field('testimonial_type');
    				<?php echo $text; ?>
    		<?php endwhile; ?>
    	<?php endif; ?>

    With the Workshop page, I want the same thing, but just the ones selected with Workshops in the Select field.

    I’ve checked for typos and done a lot of reading. Still not sure what I’m doing wrong.

    As always, any and all help is greatly appreciated.

  • Quick side note. Just for due diligence’s sake, I copy/pasted the above ‘all’ code into my Workshop page and that too gave no output. So I don’t think it’s the code.

    The only difference between Testimonials and Workshops is that Testimonials is a Page and Workshops is a Post.

    Now I really have no idea what to do?

  • You’re trying to get the values from another post, so you need to supply the post ID of that post/page

    <?php if( have_rows('testimonial_repeater', $post_id) ): ?>
    		<?php while( have_rows('testimonial_repeater', $post_id) ): .........
  • Success! Thank you so much. Just these little things, that once known, it’s “oh, of course”. Now I know. Thanks, as always, for the great support.

    Here’s the code for anyone else on here that may have run into the same issue.

    <?php if( have_rows('testimonial_repeater', 558) ): ?>
    		<?php while( have_rows('testimonial_repeater', 558) ): the_row(); 
    			$text = get_sub_field('testimonial_text');
    			<?php if( get_sub_field('testimonial_type') == 'testimonial_type_workshops' ): ?>
    					<?php echo $text; ?>
    			<?php endif; ?>
    		<?php endwhile; ?>
    <?php endif; ?>
  • Thanks. This was exactly what I was needing. I’ve extended upon this by adding an additional select to my template page (you could use also just clone the repeater select) to make the comparison IF statement reusable rather than one value. So if the 2 select values match, display data.

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

The topic ‘Only display Repeater row with certain Select value’ is closed to new replies.