Support

Account

Home Forums ACF PRO Display Specific Repeater Field Row Based on Date

Solved

Display Specific Repeater Field Row Based on Date

  • Is it possible to display a specific repeater field row, and only one row, depending on the date specified in a sub field date field?

    The following works to display all the rows greater than $today. If there are 10 rows greater than $today all 10 rows display. If 3 rows are greater than $today, all 3 rows display.

    			if( have_rows('surprise_item') ) {
    				$today = date('Ymd');	// define today's date
    			
    					while ( have_rows('surprise_item') ) : the_row();
    					  // date starting the week of surprise
    					  $startweek = get_sub_field('date');
    					  $bag_weeKstart = date_create($startweek); 
    						 
    					  $endweek = strtotime($startweek);
    					  $endweek = strtotime("+7 day", $endweek);
    					  $endweek = gmdate('Ymd', $endweek );
    					  $bag_weeKend = date_create($endweek); 
    
    				if( ($today <= $startweek) && ($today <= $endweek) )   { ?>			
    				    <h1>From <?php echo date_format($bag_weeKstart, 'M j, Y'); ?> to <?php echo date_format($bag_weeKend, 'M j, Y'); ?> your surprise is</h1>
                  	    <h2><strong>Toddler:</strong> <?php the_sub_field('toddler'); ?></h2>
                  	    <h2><strong>Baby:</strong> <?php the_sub_field('baby'); ?></h2>
                  	   <?php } ?>
                  	  <?php endwhile; //end of repeater rows ?>
          <?php } else {
    			// show if no surprise defined
    			
    			}  // end repeater rows

    Is it possible to display just one row – the first row based off a defined date field?

  • Add a break statement after showing the first. Example:

    
    if (have_rows('repeater')) {
      while (have_rows('repeater')) {
        the_row();
        if ($condition) {
          // display row
          // exit loop so no more are shown
          break;
        }
      }
    }
    
  • Perfect. Thank you!

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

The topic ‘Display Specific Repeater Field Row Based on Date’ is closed to new replies.