Home Forums Add-ons Repeater Field Advanced loop error


Advanced loop error

  • I have a repeater row setup with “if” logic that shows extra html if get_sub_field exists. The issue is that if a certain row has the if logic, the next entry that precedes it also contains that if logic, ex:

    <?php while( have_rows('resources') ): the_row(); 
    // vars
    $embed_code = get_sub_field('embed_code');
    <?php if( $embed_code ): ?>
    <?php endif; ?>

    This same logic is applied to the next entry that precedes it. That next entry doesn’t have that conditional sub field associated. Hope this makes sense. Can someone please help. Not sure if this started to happen after the latest update to ACF.

  • Hi @mbrodersen73

    Hmm… Pretty strange that the code is not working. Have you tried debugging the data in $embed_code?

    Try this, print_r(…) or echo $embed_code and share the results so that I may be able to assist.

  • did this solve your problem:

    $embed_code = '';
    $embed_code = get_sub_field('embed_code');

    i think you need to clear the variable, else it still has the value of the last entry at next loop

  • Thanks for replying James. Sorry it took so long for me to view this. Here is what is happening:

    I have a repeater setup on our corp. site. It’s a list of all of our resources. In the ACF in WordPress, I have a Repeater (resources) that has 11 sub-fields. A few of the resources are videos, so I have javascript functions that allow for lightbox video views. For the resources that are videos, I have conditional logic setup in the ACF in WordPress that says if Type is equal to video, show these 4 fields. When I fill out the forms on the WordPress page, everything looks good at this point. If I select type equal to video, the appropriate 4 fields show and I can enter the values.

    The problem is once this WordPress page is saved and I view the page, some of the other resources that are not videos have the javascript code associated with them. I thought I had it setup correct but obviously not. My php logic was to show the javascript code only if a value existed. Since all the resources that aren’t videos don’t have a particular field value, I thought that would work. Below is the actual code from the page:

    <?php if( have_rows('resources') ): ?>
            <ul id="Resource-Grid" class="list-unstyled">
              <?php while( have_rows('resources') ): the_row(); 
    				  // vars
    					$date = get_sub_field('date');
    					$featured = get_sub_field('featured');
    					$type = get_sub_field('type');
    					$topic = get_sub_field('topic');
    					$data_date = get_sub_field('data_date');
    				 	$link = get_sub_field('link');
    					$image = get_sub_field('image');
    					$title = get_sub_field('title');
    					$icon = get_sub_field('icon');
    					$download_label = get_sub_field('download_label');
    					$label_type = get_sub_field('label_type');
    					$embed_code = get_sub_field('embed_code');
    					$link_id = get_sub_field('link_id');
    					$function_id = get_sub_field('function_id');
    					$variable_id = get_sub_field('variable_id');
              <li data-date="<?php echo $data_date; ?>" class="mix <?php echo $topic; ?> <?php echo $type; ?> <?php echo $featured; ?> col-sm-6 col-lg-4">
                <?php if( $embed_code ): ?>
                <script type="text/javascript" id="vidyard_embed_code_<?php echo $embed_code; ?>" src="//<?php echo $embed_code; ?>.js?v=3.1&type=lightbox"></script> 
                <script type="text/javascript">
    			window.onload = function() {
        				var <?php echo $variable_id; ?> = document.getElementById("<?php echo $link_id; ?>");
         				<?php echo $variable_id; ?>.onclick = function() {
         					fn_vidyard_<?php echo $function_id; ?>();
         					return false;
                <?php endif; ?>
                <a <?php if( $embed_code ): ?> id="<?php echo $link_id; ?>" onclick="fn_vidyard_<?php echo $function_id; ?>();" <?php endif; ?> href="<?php echo $link; ?>" target="_blank">
                <div class="resource-item">
                  <div class="resource-img-container">
                    <div class="resource-overlay"><span class="btn btn-small white-outline"><i class="fa fa-<?php echo $icon; ?>"></i> <?php echo $download_label; ?> <?php echo $label_type; ?><span></div>
                    <img src="<?php echo $image; ?>" alt="" class="img-responsive"></div>
                  <div class="resource-content">
                    <div class="resource-date"><?php echo $date; ?></div>
                    <div class="resource-type"><?php echo $type; ?></div>
                    <p class="resource-title"><?php echo $title; ?></p>
                </a> </li>
              <?php endwhile; ?>
            <?php endif; ?>

    Using Developer Tools, here is what is showing in the rendered HTML for a resource whose type is NOT equal to video:

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

The topic ‘Advanced loop error’ is closed to new replies.