我正在尝试隐藏自定义日期字段早于今天日期的帖子。我的代码当前设置的方式是将一个名为expired的类添加到LI标记中,如果它更旧但是它没有播放...
<?php
wp_reset_query();
query_posts(array('post_type' => 'events',
'showposts' => 5,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => ASC));
?>
<?php while (have_posts()) : the_post(); ?>
<?php
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
$currentdate = date("Ymd");
?>
<li class="<? if ($eventDate < $currentdate) { echo "expired"; } ?>">
<h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
<span class="date"><strong>Event Date:</strong> <? echo $eventDate->format('d/m/Y'); ?></span>
</li>
<?php endwhile;?>
请帮助我某人:(
答案 0 :(得分:2)
目前您正在将DateTime对象与字符串进行比较 - 您需要确保比较等效数据类型:
// Convert stored date to DateTime object
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
// Get the current date as a DateTime object
$nowDate = new DateTime();
// And compare them
if ($eventDate == $nowDate) {
// They're the same, woohoo
} elseif ($eventDate < $nowDate) {
// Expired
}