Support

Account

Home Forums Add-ons Repeater Field Unable to get the repeater field working (Never passes has_sub_field while loop)

Solved

Unable to get the repeater field working (Never passes has_sub_field while loop)

  • Somehow i am out of ideas, and i guess i do something completely wrong. I try to get the repeater field running, but somehow my loop fails. But honestly i have no idea why.

    Post Type: projects
    Repeater Field Name: pro_repeatertest
    Repeater Subfield Name: pro_food (listed in the “repeater fields” of pro_repeatertest)

    the loop looks like that:

    
    <?php
    	$args = array(
    		'post_type' => 'projects',
    		'posts_per_page' => 3
    	);
    	$the_query = new WP_Query( $args );
    ?>
    <? if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
    	<p><?php the_field( 'pro_subline' ); ?></p>
    	<?php if(get_field( 'pro_repeatertest' ) ): ?>
    		<?php echo 'We are in the if loop'; ?>
    		<ul>
    		<?php while( has_sub_field( 'pro_food' ) ): ?>
    			<?php echo 'We are in the while loop'; ?>
    
    			<li><?php the_sub_field( 'pro_food' ); ?></li>
    		<?php endwhile; ?>
    			<?php echo 'We get out of the while loop'; ?>
    		</ul>
    	<?php endif; ?>
    <?php endwhile; ?>
    	<?php wp_reset_postdata(); ?>
    <?php else: ?>
    	<p>There are no posts or pages here</p>
    <?php endif; ?>
    

    The odd thing is i only see the following echos:

    We are in the if loop
    We get out of the while loop

    But the: “We are in the while loop” is missing. seems the has_sub_field ‘pro_food’ while loop is never passed. Maybe something is wrong about the syntax? But it is the same like in the docs and the name of the repeater sub field is correct. Any suggestions are more than welcome. 😉 Thanks in advance… Best regards Ralf

  • Hi @rpk

    I think the issue is that you are using the wrong field name for the has_sub_field loop.

    This function requires the field name to be the ‘repeater field’ name, not one of the sub fields.

    Your code:

    
    <?php if(get_field( 'pro_repeatertest' ) ): ?>
    		<?php echo 'We are in the if loop'; ?>
    		<ul>
    		<?php while( has_sub_field( 'pro_food' ) ): ?>
    

    Should be changed to:

    
    <?php if(get_field( 'pro_repeatertest' ) ): ?>
    		<?php echo 'We are in the if loop'; ?>
    		<ul>
    		<?php while( has_sub_field( 'pro_repeatertest' ) ): ?>
    

    Thanks
    E

  • ahhhhhhhhhhhhhhhh as i suspected, i got something horrible wrong with my interpretation of the docs examples. thanks!!! it’s working now.

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

The topic ‘Unable to get the repeater field working (Never passes has_sub_field while loop)’ is closed to new replies.