Support

Account

Home Forums Add-ons Repeater Field Getting The First Two Rows of a Repeater and Then Outputting a Nested Repeater

Solved

Getting The First Two Rows of a Repeater and Then Outputting a Nested Repeater

  • I’m in a little over my head on this one. I’m try to do two different tasks here:

    1. Get the first row of a repeater. Got that working.

    2. There’s a nested repeater in each of that row. I want to get that and loop through, but not sure of the syntax.

    Outer repeater is “press_releases”
    Inner repeater is “pr_posts”
    My code:

    <?php if( have_rows('press_releases') ): ?>
    <h2>Press Releases <span class="sprite icon"></span></h2>
    <div>
    <?php endif; ?>
    
    <?php
       $prRows = get_field('press_releases' ); // get all the rows
       $first_row = $prRows[0]; // get the first row
       $first_row_year = $first_row['year'];
       $first_row_pr = $first_row['pr_posts'];
    ?>
    
    <h3><?php echo $first_row_year; ?></h3>
    
    <?php
       $pr_posts = get_field('pr_posts' ); // get all the rows
       $pr_rows = $pr_posts[0]; // get the first row
    									   
       if($pr_rows) {
       foreach($pr_rows as $rows)
          {
             echo '<h4>Test</h4>';
          }
       }
    ?>
  • Working with nested repeaters is a large topic. Have you seen this tutorial http://www.advancedcustomfields.com/resources/working-with-nested-repeaters/

    Let me know if that doesn’t help.

  • John,
    Yeah, the complication that’s throwing me through a loop (pun intended) is the getting the first row and then the syntax to loop through that nested repeater.

  • Just typing it up here and formatting it helped me work it out in my head:

    <?php if( have_rows('press_releases') ): ?>
    <h2>Press Releases <span class="sprite icon"></span></h2>
    <div>
    <?php endif; ?>
    
    <?php
       $prRows = get_field('press_releases' ); // get all the rows
       $first_row = $prRows[0]; // get the first row
       $first_row_year = $first_row['year'];
       $first_row_pr = $first_row['pr_posts'];
    ?>
    
    <h3><?php echo $first_row_year; ?></h3>
    
    <?php
       if($first_row_pr) 
          {
             foreach($first_row_pr as $pr)
             {
                echo '<h4><a href="#">' . $pr['heading'] . '</a></h4>';
                echo '<p>' . $pr['date'] . '</p>';
             }
          }
    ?>
  • Edited: answered it yourself before I could finish typing. Sometimes typing it out does help. That’s how I do my thinking most of the time.

    …..

    Maybe this code for a nested repeater stripped of all the extra stuff will help.

    
    if (have_rows('parent_repeater')) {
        while (have_rows('parent_repeater')) {
            the_row();
            if (have_rows('nested_repeater')) {
                while (have_rows('nested_repeater')) {
                    the_row();
                    $value = get_sub_field('sub_field_of_nested_repeater');
                }
            }
        }
    }
    

    So basically you have two nested loops that are identical.

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

The topic ‘Getting The First Two Rows of a Repeater and Then Outputting a Nested Repeater’ is closed to new replies.