我正在使用ACF创建自定义wordpress字段。使用 repeater
字段,我可以使用相同的字段添加多个内容。目前,我确实在我的中继器字段dates_list
中存储了两个变量:
-
si-date
– 日期(格式 20160225) -
si-special
– 随机文本
我可以将中继器的所有行存储在 var 中并选择某行dates_list
:
<?php
$rows = get_field('dates_list' ); // get all the rows
$first_row = $rows[0]; // get the first row
$first_row_image = $first_row['si-date' ]; // get the sub field value
?>
如何使用此方法比较si-date
中的所有日期并选择包含下一个即将到来的日期的行?
如果您只需要获取今天之后的下一个日期,您可以做的是引用此处解释得很好的 usort 函数,以按最近的日期排序: 使用 DateTime 字符串对数组进行排序?
以这种方式对数组进行排序后,就可以处理该数组以查找大于今天的第一个日期。也许像这样:
foreach($sorted_dates_array as $row){
//If the date is greater than the current time then return the row
if(strtotime($row['si-date'])>time())
{
return $row;
}
//There are no dates that are yet to be completed
return false;
}
您必须遍历所有行并"手动"检查它。我没有测试过这段代码,但我认为这可能是一个好的开始:
<?php
$rows = get_field('dates_list');
$actual_date = date('Ymd');
$selected_date = '99999999';
$selected_row = null;
foreach ($rows as $row) {
if ($row['si-date'] > $actual_date AND $row['si-date'] < $selected_date) {
$selected_date = $row['si-date'];
$selected_row = $row;
}
}
if (is_array($selected_row)) {
// do your stuff your with the row
}
?>