Home Forums ACF PRO Query field that lives outside the repeater loop


Query field that lives outside the repeater loop

  • Hi there,

    I have a repeater loop (if, while) to get the sub_fields. However, i’d like to make an if-statement from an option that’s selected outside the repeater fields.

    So it’s something like:

    <?php if ( have_rows( 'expertises_all' ) ) : ?>
    	<?php while ( have_rows( 'expertises_all' ) ) :
    		the_row(); ?>
    		<div class="single-expertise">
    			<img src="<?php the_sub_field( 'icon' ); ?>" alt="">
    			<?php if ( $title = get_sub_field( 'title' ) ) : ?>
    				<h3><?php echo $title; ?></h3>
    			<?php endif; ?>
    			<?php if ( get_sub_field( 'display' ) == 'view-all' ) : ?>
    				<?php if ( $description = get_sub_field( 'description' ) ) : ?>
    					<?php echo $description; ?>
    				<?php endif; ?>
    			<?php endif; ?>
    	<?php endwhile; ?>
    <?php endif; ?>

    It regards the “get_sub_field (‘display’ == ‘view-all’ )”, which lives outside the repeater loop.

    I thought of querying based on the post ID, but the element could show up more than once on the same page so I guess that would mess up the results?

    Any suggestions?

  • if the field “display” in not in the repeater then use get_field('display')

  • I don’t think it works because it lives inside another Flexible Content row (should have specified), which also requires get_sub_field – right?

  • If this is a nested repeater/flex situation, you cannot get fields from the parent repeater/flex field. You have to get these values before the nested loop of you want to use them in the nested loop. Let me know if you understand that or you need an example.

  • I solved it by creating a CSS class from the value before the loop, then hide/show it based on that class.

    Not ideal as it’s unnecessary HTML, but works for this situation.

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

You must be logged in to reply to this topic.