Support

Account

Home Forums ACF PRO Only showing 2 of 3 items in Post_Object Repeater

Solving

Only showing 2 of 3 items in Post_Object Repeater

  • Hi there,
    I’ve got an odd problem here (I think). I’m using a Repeater field to choose post objects from a CPT. When I go to echo the items saved to the repeater within the body of my page, it echos all 3 just fine and dandy. But when I use the exact same code within my header to echo those items to the nav bar, it’s only showing the most recent 2. When I do a print_r for those nav items, it only shows two, but seems to indicate that there are 3 (unless I’m understanding the output wrong). And when I query for the number of rows in that repeater in order to set the correct CSS, it’s definitely seeing all 3.

    Here’s the code within the body that’s showing all 3 items just fine:

    if( get_field('home_or_on_tour', 'option') == 'tour' ): ?>
    
    Please choose the location you wish to attend
    
    <?php if( have_rows('locationsdates', 'option') ):
    
     	// loop through the rows of data
        while ( have_rows('locationsdates', 'option') ) : the_row();
    
            // display a sub field value
            $post_id = get_sub_field('location', false, false);
            
            if($post_id): ?>
            <a href="<?php echo get_the_permalink($post_id); ?>"><?php echo get_the_title($post_id); ?></a>
            Event Date: <?php the_field('event_date', $post_id); ?>
            <?php $featured_img_url = get_the_post_thumbnail_url($post_id,'thumbnail'); ?>
            <img src="<?php echo $featured_img_url; ?>">
            <?php echo $post_id ?>
    
            <?php endif;
    
        endwhile;
    
    	else :
    
        // no rows found
    
    endif;
    
    endif; ?>

    Here’s the code for the CSS that’s working (I’m counting the rows in order to divide the nav bar area by the correct number of items)

    if( get_field('home_or_on_tour', 'option') == 'tour' ):
    				//$location = get_sub_field('location');
    				if( have_rows('locationsdates', 'option') ): the_row();
    				$count = count(get_field('locationsdates', 'option'));
    				//	$locationcount = count($location);
    			?>
    			@media (min-width: 768px) {
    				.navbar-right li {
    					width: -webkit-calc(100% / <?php echo $count; ?>); /** Safari 6, Chrome 19-25 **/
    					width: -moz-calc(100% / <?php echo $count; ?>); /** FF 4-15  **/
    					width: calc(100% / <?php echo $count; ?>); /** FF 16+, IE 9+, Opera 15, Chrome 26+, Safari 7 and future other browsers **/
    				}
    			}
    				<?php endif;
    			endif;

    And here’s the code for the nav bar, which is only giving me 2 of the 3 items:

    if( get_field('home_or_on_tour', 'option') == 'tour' ):
    										if( have_rows('locationsdates', 'option') ): 
    											while( have_rows('locationsdates', 'option') ): the_row();
    											
    												$location_id = get_sub_field('location', false, false);
    											
    												if($location_id): //$l=0;
    													$location = $location_id;
    													
    													setup_postdata( $location );
    									?>
    									<li><a href="<?php echo get_the_permalink($location_id); ?>" title="<?php echo get_the_title($location_id); ?>" ><?php echo get_the_title($location_id); ?></a></li>
    												<?php wp_reset_postdata();
    												endif;
    											endwhile;
    										endif;
    									endif;

    I’ve tried it without the setup_postdata/wp_reset_postdata() and get the exact same results. I’ve tried putting the nav query inside a regular wp loop (obviously didn’t work).

    When I do a print_r, I get this:

    <pre>string(3) "239"</pre>
    <pre>string(3) "240"</pre>

    The item not showing is “238”.

    I’ve now wasted at least 3 hours trying to figure out what I’m doing wrong, and am hoping that someone here will see immediately what I’m not seeing.

    Thanks in advance.

    ~Laura

  • You have a couple issues

    
    if( get_field('home_or_on_tour', 'option') == 'tour' ):
      // do this before calling have_rows()
      // calling get_field on a repeater after using have_rows()
      // will screw up the loop
      $count = count(get_field('locationsdates', 'option'));
      if( have_rows('locationsdates', 'option') ): 
        // missing while
        while ('locationsdates', 'option') ): 
          the_row();
    
  • In addition to the above, if you are looping over the same repeater twice in the same page you need to reset rows before the end of the first loop

    
    // first loop on repeater
    if (have_rows(......)) {
      // rest of your code
      reset_rows();
    } // end if
    
  • Hmmm…except that the if( get_field(‘home_or_on_tour’, ‘option’) == ‘tour’ ): isn’t the repeater. I’m checking first to see if “tour” was selected in an option dropdown. The repeater appears conditionally, depending on which item is selected there. However, I’d never seen the reset_rows code, so I will give that a try as soon as I’m back at my computer. THANK YOU!!

  • AHA! Adding the reset_rows() to the one with the count seems to have fixed it! Thank you very much!

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

The topic ‘Only showing 2 of 3 items in Post_Object Repeater’ is closed to new replies.