我有一個帶有日期子欄位的 ACF 中繼器欄位,用于在旅行社網站上添加出發日期。在前端,我有一個標簽組件,用于按月顯示/過濾出發日期。
為了構建組件,我檢查行以創建僅回傳格式化月份的過濾選單。
<?php if( have_rows('departure_dates') ): ?>
<div class="tour__dates-filter js-dates">
<?php while( have_rows('departure_dates') ): the_row(); ?>
<?php
$dateMonth = get_sub_field('date', false, false);
$dateNew = new DateTime($dateMonth);
?>
<button class="tour__dates-filter__item"><span><?php echo $dateNew->format('M'); ?></span></button>
<?php endwhile; ?>
</div>
<?php endif; ?>
然后我再次檢查行以構建日期串列,而不格式化回傳的日期:
<?php if( have_rows('departure_dates') ): ?>
<div class="tour__dates-filter js-dates">
<?php while( have_rows('departure_dates') ): the_row(); ?>
<div class="tour__dates-table__col-cel">
<?php $date = get_sub_field('date'); ?>
<?php if( $date ): ?>
<?php echo $date; ?>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
我遇到的問題是過濾選單上顯示了多個出發日期的月份。我只想顯示每個月的第一行有一個條目
所以這:
<button>JAN</button>
<button>JAN</button>
<button>JAN</button>
<button>FEB</button>
<button>FEB</button>
<button>MAR</button>
變成這樣:
<button>JAN</button>
<button>FEB</button>
<button>MAR</button>
我如何只回傳每個月的第一個條目?任何幫助都會很棒
提前謝謝了
uj5u.com熱心網友回復:
您可以跟蹤您回圈的內容并有條件地顯示,如下所示:
<?php if (have_rows('departure_dates')) : ?>
<div class="tour__dates-filter js-dates">
<?php
$found_months = [];
?>
<?php while (have_rows('departure_dates')) : the_row(); ?>
<?php
$dateMonth = get_sub_field('date', false, false);
$dateNew = new DateTime($dateMonth);
$current_month = $dateNew->format('M');
?>
<?php if (!in_array($current_month, $found_months)) : ?>
<button class="tour__dates-filter__item"><span><?php echo $current_month; ?></span></button>
<?php endif; ?>
<?php
$found_months[] = $current_month;
?>
<?php endwhile; ?>
</div>
<?php endif; ?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/523053.html
下一篇:為訪客用戶顯示某些類別的產品價格
