【发布时间】:2025-12-05 22:45:01
【问题描述】:
我有一个名为 Classes 的 CPT。通过 ACF 关系字段,我允许我的客户手动选择要在前端显示的课程。每个班级都有一个到期日。
在 foreach 语句中,我设置了一个条件,将当前日期与到期日期进行比较,并且只显示即将到来的课程。我需要的是在所有选定的课程都超过其到期日期后显示一个说明“没有即将上课的课程”。
ACF 支持建议在 foreach 循环中添加增量运算符,然后检查该值是否为空。他们修改了我的代码如下,但它没有完成这项工作。 ACF 支持提供的其他帮助超出了他们提供的范围,因此我在此处发布以获取指导。谢谢!
<?php
$all_classes = get_sub_field('class');
if( $all_classes ):
?>
<?php
$i = 0;
foreach($all_classes as $post):
setup_postdata($post);
?>
<?php
$now = time(); // get today's date
$expiry_date = strtotime(get_field('class-expiry-date')); // get the expiration date
if ($now < $expiry_date): // compare the dates and show upcoming classes only
$i++;
?>
class details
<?php endif; ?>
<?php
endforeach;
wp_reset_postdata();
?>
<?php else: ?>
<?php
//check if $i is empty
if(empty($i)):
?>
There are no upcoming classes.
<?php endif; ?>
<?php endif; ?>
【问题讨论】:
标签: php conditional-statements advanced-custom-fields