Support

Account

Home Forums Add-ons Repeater Field 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 depending on the month range specified in a sub field number field? The repeaters belong to a specific page ID 25.

    Repeater: contact_hours
    Subs:
    date_range_beginning (number field)
    date_range_ending (number field)
    contact_hour_days (text field)
    contact_hour_hours (text field)

    My current query works to display all rows fine but I need to only display the date range that is current. So if the row’s date range beginning and ending is today, display that row subs only.

    Is this possible/feasible or should I tackle this a different way? WP 4.4.2 and ACF 5.3.6.1 Thanks for any help!

    
    <?php if( have_rows('contact_hours', 25) ): ?>
        <div class="footer-hours">
    	    <div class="footer-hours-title">Hours</div>
    	    <?php while ( have_rows('contact_hours', 25) ) : the_row(); ?>
    	        <div class="footer-hour-segment">
    		        <div class="contact-hour-days"><?php the_sub_field('contact_hour_days', 25); ?></div>
    		        <div class="contact-hour-hours"><?php the_sub_field('contact_hour_hours', 25); ?></div>
    	        </div>
    	    <?php endwhile; ?>
        </div><!--footer-hours-->
    <?php endif; ?>
    
    
  • Hi @inhouse

    I believe you can do it like this:

    $current_date = (int) date('j');
    $start = (int) get_sub_field('date_range_beginning');
    $end = (int) get_sub_field('date_range_ending');
    
    if ($current_date >= $start && $current_date <= $end){
        the_sub_field('contact_hour_days', 25);
    }

    Hope this helps.

  • Thanks @james you rock! That worked great. For anyone interested, I’m posting my complete code below.

    
    <?php if( have_rows('contact_hours', 25) ): ?>
        <div class="footer-hours">
    	    <div class="footer-hours-title">Hours</div>
    	    <?php while ( have_rows('contact_hours', 25) ) : the_row(); ?>
    	        <?php
    	        $begin_date = DateTime::createFromFormat('m', get_sub_field('date_range_beginning'));
    			$end_date = DateTime::createFromFormat('m', get_sub_field('date_range_ending'));
    	        $current_date = (int) date('m');
    			$start = (int) get_sub_field('date_range_beginning');
    			$end = (int) get_sub_field('date_range_ending');
    			if ($current_date >= $start && $current_date <= $end) { ?>
    		        <div class="footer-hour-segment">
    			        <div class="contact-range">
    					    <div class="contact-date-range"><?php echo $begin_date->format('F'); ?> - <?php echo $end_date->format('F'); ?></div>
    					    <div class="contact-hour-days"><?php the_sub_field('contact_hour_days', 25); ?></div>
    				        <div class="contact-hour-hours"><?php the_sub_field('contact_hour_hours', 25); ?></div>
    			        </div>
    		        </div>
    	        <?php } ?>
    	    <?php endwhile; ?>
        </div><!--footer-hours-->
    <?php endif; ?>
    
Viewing 3 posts - 1 through 3 (of 3 total)

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