Home Forums ACF PRO If next date in loop…


If next date in loop…

  • So, I’ve got a query of product items. Within each product I have a “date” field (each product is actually an event). My query is ordering everything very well, but I’m using a Bootstrap vertical tabs layout and want to set the tab for the most upcoming date to be set to active. I’m not having any luck setting this though. I’ve currently just got it at > $currentDate, and obviously that’s not going to work since all events/products are in the future right now. I *only* want the one with the soonest event to be “active”.

    I’m not sure really if this is a WP question, a PHP question, or an ACF question, so I apologize if I’m asking in the wrong place.

    Here’s my code for the first part of the tab (since it will duplicate for the other part)

    <div class="nav flex-column nav-pills" id="v-pills-tab" role="tablist" aria-orientation="vertical">
    					<?php $args_left = array(
    						'post_type' => 'product',
    						'posts_per_page' => 999,
    						'product_cat' => 'current-season',
    						'meta_key' => 'date',
    						'orderby' => 'meta_value_num',
    						'order' => 'ASC'
    					$loop_left = new WP_Query( $args_left );
    					$counter = 0;
    					while ( $loop_left->have_posts() ) : $loop_left->the_post(); global $product;
    					$today = date('Ymd');
    					$date = get_field('date');
    					$date = str_replace('/', '-', $date);
    					$date_timestamp = strtotime($date);
    					$series = get_field('concert_series');
    					<?php // temp disable - if ($date_timestamp > strtotime($today)) { ?>
    					<?php // temp disable - } else { ?><?php // temp disable - } ?>
    					<a class="nav-link <?php if ($date > $today) { echo 'active'; } ?>" id="v-pills-<?php echo $counter; ?>-tab" data-toggle="pill" href="#v-pills-<?php echo $counter; ?>" role="tab" aria-controls="v-pills-<?php echo $counter; ?>" aria-selected="<?php if ($date_timestamp < strtotime($today)) { ?>true<?php } else { ?>false<?php } ?>">
    						<h3><?php echo date('M', $date_timestamp); ?>. <span><?php echo date('d', $date_timestamp); ?>, <?php echo date('Y', $date_timestamp); ?></span></h3>
    						<p><?php echo $series['label']; ?></p>
    					<?php $counter++;
    					endwhile; ?>
  • It’a more or a general PHP/WP question.

    It looks like you are setting every “tab” to active. Basically, if you have you posts ordered so that they are all in the future then the first one will always be the soonest event. You already have a counter so I think this should work
    <?php if ($counter == 0) { echo 'active'; }

  • Well, not really. If I just wanted the first one to be “active”, that would work fine. But I want the *next* one to be active.So, let’s say the first event is on September 23rd, the next on October 21st, but today is September 25th. I don’t want the September 23rd one to be active anymore. I want the October 21st one to be active. See what I mean?

  • Note: The reason I’m thinking this still falls under ACF is that the date field is what I’m dealing with.

  • In your code, before the loop add another flag

    $active_shown = false;

    Then when you show won as active, set the flag to true

    <?php if ($date > $today && !$active_shown ) { echo 'active'; $active_shown = true; } ?>
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘If next date in loop…’ is closed to new replies.