Support

Account

Home Forums Add-ons Repeater Field Repeater not working

Solved

Repeater not working

  • Please help
    I have a repeater on single custom post type “news” inside the loop as:

    if (have_posts()) : while (have_posts()) : the_post();

    The code for the repeater is:

    <?php if( have_rows(‘cobertura’) ): ?>
    <?php while( have_rows(‘cobertura’) ): the_row(); ?>
    <?php the_sub_field(‘pais’); ?> /
    <?php the_sub_field(‘estado’); ?> /
    <?php the_sub_field(‘ciudad’); ?> /
    <?php endwhile; ?>
    <?php endif; ?>

    I got an error and the page stops loading the rest of the content, if I change the_sub_filed for get_sub_field function it return Array for each saved value but I dont know how to echo the values i want

  • are each of the sub fields pais, estado, ciudad nested repeaters? If not, what type of fields are they?

  • I tried with text subfield and it is working but not with the post objet subfields

  • A post object returns either a post object or a post IDs or an array of post objects or post IDsan array of values or a post object depending on how you have the field set up. https://www.advancedcustomfields.com/resources/post-object/

  • They are post objets, I just fixed those:

    if( have_rows(‘cobertura’) ): ?>
    <p class=”font-arial fs-13 hint-text”>País<br>
    <?php while ( have_rows(‘cobertura’) ) : the_row();
    $myposts = get_sub_field(‘pais’);
    if( $myposts ): foreach( $myposts as $post_object):
    echo get_the_title($post_object->ID);
    endforeach;
    endif;
    echo “
    </p>”;
    endwhile;
    else :
    if ($pais != ”) { ?><p class=”font-arial fs-13 hint-text”>País<br><?php echo $pais; ?></p><?php }
    endif; ?>

    Now I have another repeater “temas” located right next to “cobertura” from where I get “temas_generales” and “temas_específicos” both are post object subfields, I did copy & paste the code for “cobertura” and change only the names of the fields, but it returns the title of the custom post type (news), any idea?

  • can you post the code you’re having a problem with? Please you the “code” option instead of just pasting it. Also, you’ll need to make sure I understand what types of fields you’re dealing with, it’s not obvious to me by the field names.

  • Hi John, I got working the first loop, now Im getting the values for the “temas” but theres printing another value duplicated by 3 on each subfield right next to “temas_generales” and “temas_especificos”. I dont know where is coming from, here is the code (sorry for just pasting the code before)

    “temas” is a repeater that has 2 subfields “tema_general” and “tema_especifico” both are Post Object defined as Post Object in the Return Format field

    if( have_rows('cobertura') ): ?> 
    <p class="font-arial fs-13 hint-text">País(es)<br><strong>
    <?php while ( have_rows('cobertura') ) : the_row(); 
    $myposts = get_sub_field('pais');
    if( $myposts ):  foreach( $myposts as $post_object):
       echo get_the_title($post_object->ID); 
    endforeach; 
      endif;
      echo "</strong></p>";
          endwhile;
        else :   
          if ($pais != '') { ?><p class="font-arial fs-13 hint-text">País<br><strong><?php echo $pais; ?></strong></p><?php } 
        endif;  
    
        
    if( have_rows('temas') ):  ?>
    <p class="font-arial fs-13 hint-text">Tema(s)<br><strong>
    <?php   while ( have_rows('temas') ) : the_row(); 
    
    $post_objects = get_sub_field('tema_general');
    
    if( $post_objects ): ?>
        
        <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
          
                <?php the_title(); ?>
               
           
      <?php endforeach; ?>
        
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;
    
    $post_objects = get_sub_field('tema_especifico');
    
    if( $post_objects ): ?>
        
        <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
          
                <?php the_title(); ?>
               
           
      <?php endforeach; ?>
        
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;
      echo "</strong></p>";
    
       endwhile;
        else :   
    
        endif;  ?>
  • Ok I fixed this way, getting the subfields separated

    <?php if( have_rows('temas') ) { ?>
    <p class="font-arial fs-13 hint-text">Tema(s) general(es)<br><strong>
        <?php while( have_rows('temas') ): the_row(); ?>
            <?php $post_object = get_sub_field('tema_general'); ?>
            <?php if( $post_object ): ?>
                <?php // override $post
                $post = $post_object;
                setup_postdata( $post );
                ?>
                <li><?php the_title(); ?></li>
                <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
            <?php endif; ?>
        <?php endwhile; ?>
        <?php echo "</strong></p>"; ?>
    <?php } ?>
    
    <?php if( have_rows('temas') ) { ?>
    <p class="font-arial fs-13 hint-text">Tema(s) especifico(s)<br><strong>
        <?php while( have_rows('temas') ): the_row(); ?>
            <?php $post_object = get_sub_field('tema_especifico'); ?>
            <?php if( $post_object ): ?>
                <?php // override $post
                $post = $post_object;
                setup_postdata( $post );
                ?>
                <li><?php the_title(); ?></li>
                <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
            <?php endif; ?>
        <?php endwhile; ?>
        <?php echo "</strong></p>"; ?>
    <?php } else {  if ($tg != '') { ?><p class="font-arial fs-13 hint-text">Temática general <br><strong><?php echo $tg; ?></strong></p><?php } 
     if ($te != '') { ?><p class="font-arial fs-13 hint-text">Temática específica<br><strong><?php echo $te; ?></strong></p><?php }
    } ?>

    Now I want to echo the label Tema(s) especific(os) and Tema(s) general(es) only if a specific sub field exist, since now is printing in the case if any of the 2 subfields exists, thank you!

  • 
    <?php 
      if( have_rows('temas') ) { 
        ?>
          <p class="font-arial fs-13 hint-text">Tema(s) general(es)<br><strong>
            <?php 
              // keep a count of tema_especifico
              $tema_especifico = 0;
              while( have_rows('temas') ): 
                the_row();
                // see if row has tema_especifico
                if (get_sub_field('tema_especifico')) {
                  // yes add to count
                  $tema_especifico++;
                }
                $post_object = get_sub_field('tema_general');
                if( $post_object ): 
                  // override $post
                   $post = $post_object;
                  setup_postdata( $post );
                  ?>
                  <li><?php the_title(); ?></li>
                  <?php 
                    wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
                endif;
              endwhile;
            echo "</strong></p>";
      }
      
      if( have_rows('temas') && $tema_especifico ) { 
        // only show this section if tema_especifico were found during first loop
        ?>
          <p class="font-arial fs-13 hint-text">Tema(s) especifico(s)<br><strong>
            <?php 
            while( have_rows('temas') ): 
              the_row();
              $post_object = get_sub_field('tema_especifico');
              if( $post_object ):
                // override $post
                $post = $post_object;
                setup_postdata( $post );
                ?>
                  <li><?php the_title(); ?></li>
                <?php 
                wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly
              endif;
            endwhile;
          echo "</strong></p>";
      } else {
        if ($tg != '') {
          ?>
            <p class="font-arial fs-13 hint-text">
              Temática general <br><strong><?php echo $tg; ?></strong>
            </p>
          <?php
        } 
         if ($te != '') { 
          ?>
            <p class="font-arial fs-13 hint-text">
              Temática específica<br><strong><?php echo $te; ?></strong>
            </p>
          <?php 
        }
      } 
    ?>
    
  • It works great, thank you very much!!!!
    Best regards
    V

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

You must be logged in to reply to this topic.