Home › Forums › Add-ons › Repeater Field › 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; ?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.