Support

Account

Home Forums Add-ons Repeater Field Problema withe repeater item

Solved

Problema withe repeater item

  • I have a code that kinda works. It gets the repeater and shows everything from the article that I need.

    The problem is that it’s not looping trouhg the repeater ‘bloco’ it only runs it once.

    Here is the code:

    http://pastebin.com/Uy9HdP3C

  • I’ve pared down your code to the important bits to explain where the problem is. Hope it helps

    
    <?php
      // there is already a query running, the main query
      
      // this is a nested secondary query
      $the_query = new WP_Query(array(
          // .. your query here
      ));
      while( $the_query->have_posts() ) : $the_query->the_post();
        // .. do some stuff
        if( have_rows('bloco') ): 
          while ( have_rows('bloco') ) : 
            the_row();
            $post_objects = get_sub_field('artigos');
            if( $post_objects ): 
            
              // temporarily hold the post from your secondary query
              // see comment below about problem doing wp_reset_postdata();
              $temp_post = $post;
              
              foreach( $post_objects as $post): 
                setup_postdata($post); 
                // ... do some stuff
              endforeach;
              
              // you should do a wp_reset_postdata() here
              // however, if you do that you will reset post data
              // to the main query and not your secondary nested query
              // so get the value we stored above here
              $post = $temp_post;
              
            endif; //if( $post_objects ): foreach( $post_objects as $post): setup_postdata($post);
          endwhile;
        endif; //<?php if( have_rows('bloco') ): while ( have_rows('bloco') ) : the_row();
      endwhile; // end of the secondary query loop
      
      // this resets post data to main query
      wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
    ?>
    
  • Thnx a lot dude, it worked like a charm.

  • Hello.

    I working on a similar problem for some hours now, I just can’t find the solution.
    I tried the code above, only the first repeater element is displayed, followed by weird posts, I dont even know where they came from.

    Here’s my code :
    // main query
    <?php $args = array (‘post_type’ => ‘newslettre’, ‘order’ => ‘DESC’,’posts_per_page’ => ‘1’, );
    $my_query= new WP_Query( $args );
    while ( $my_query->have_posts() ) : $my_query->the_post();

    // repeater
    if( have_rows(‘articles’) ):
    while ( have_rows(‘articles’) ) : the_row();
    $post_object = get_sub_field(‘choisir_un_article’);
    if( $post_object ):
    $temp_post = $post;

    foreach( $post_object as $post):
    setup_postdata($post);
    ?>

    // … do some stuff

    <?php endforeach;
    $post = $temp_post; ?>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php endif; ?>

    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>

    Your hekl would be very appreciated !!!

    Thank you

  • @hube2 Thank you but it does not work for me :/

    I try other loop but I have juste 1 post in Front

    An idea ?

    Thank you so much

    <?php $query = new WP_Query( array( 'post_type' => 'nosmarques' ) );                
    if ( $query->have_posts() ) : ?>
    	<?php while ( $query->have_posts() ) : $query->the_post(); ?>   
    		<div id="popup<?php the_ID(); ?>" class="popup_block">
    			<div class="center">
    				<?php the_post_thumbnail(); ?>
    				<span class="h1 dispo"><?php _e( 'Disponibilités', 'territoiredhomme' ); ?></span>
    			</div>
    			<?php if( have_rows('brand_in_shop') ): ?>
    				<ul class="shop-brand globe">
    					<?php while ( have_rows('brand_in_shop') ) : the_row(); ?>   
    						<li class="inline-block">
    						    <?php $post_object = get_sub_field('shop_sales'); ?>
    						    <?php if( $post_object ): ?>
    						 		<?php $post = $post_object; setup_postdata( $post ); ?>
    						 			<a class="uppercase" href="territoiredhomme/nos-boutiques/">
    						                <?php the_title(); ?>
    						            </a>
    						        <?php wp_reset_postdata(); ?>
    						    <?php endif; ?>
    						</li>	 
    					<?php endwhile; ?>	 
    				</ul>
    			<?php endif; ?>		 
    		</div>	
    	<?php endwhile; wp_reset_postdata(); ?>
    	<!-- show pagination here -->
    	<?php else : ?>
    	<!-- show 404 error here -->
    <?php endif; ?>
  • The problem is as I have explained in other places on these forums, and above. You cannot use a nested loop that includes setup_postdata() and wp_reset_postdata(). wp_reset_postdata() ALWAYS resets post data to the main query. In you example there is the “Main WP Query”, then you do a custom query, then you get a post from a field. The last is the 3rd nesting. Calling wp_reset_postdata() here is what is causing the problem. You must interact with this post in another way, for example

    
    <li class="inline-block">
      <?php 
        $post_object = get_sub_field('shop_sales');
        if ($post_object) {
          ?>
            <a class="uppercase" href="territoiredhomme/nos-boutiques/">
              <?php echo get_the_title($post_object->ID); ?>
            </a>
          <?php 
        }
      ?>
    </li>
    
  • It’s ok !
    Thank you for your answer !

    I have understant for the setup_postdata() and wp_reset_postdata().

    have a good day !

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

The topic ‘Problema withe repeater item’ is closed to new replies.