1

我正在尝试隐藏自定义日期字段早于今天日期的帖子。我的代码当前设置的方式是在 LI 标签中添加一个名为 expired 的类,如果较旧但它不玩球......

            <?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;?>

请帮帮我:(

4

1 回答 1

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
}
于 2013-02-23T14:53:30.213 回答