我试图在页面上显示尚未过期的项目,但即使其未过期,该项目也始终为空。请注意,过期日期自定义字段是通过acf创建的,保存格式为dd/mm/yy
<?php
$args = array('post_type' => 'fooditems', 'numberposts' => -1);
$todaydate=new DateTime();
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$expirydate=DateTime::createFromFormat('d/m/y', get_field('expirydate',$post->ID));
if ($todaydate <= $expirydate) {
?>
<div class="index">
<h4> <?php echo the_title(); ?> </h4>
<div class="thumb"><?php the_post_thumbnail('')?></div>
</div>
<?php } endwhile; ?>
好的,我找到了解决方案。。。是$todaydate=new DateTime();
。。。我刚把它改成$todaydate=date('d/m/y')
,它就起作用了。