2013-02-23 59 views
1

我试图隐藏帖子,其中自定义日期字段比今天的日期更早。我的代码目前正在建立的方式是,如果旧的,但它不是打球添加一个名为过期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

被如何存储您的自定义日期(即什么格式)是否'get_field(” EVENT_DATE“) '绝对有效吗? – hohner 2013-02-23 14:33:08

+0

我相信它存储为201这样的整数30223,然后将其转换为可读格式23/02/2013 – Amesey 2013-02-23 14:46:17

回答

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 
} 
+0

谢谢,但现在返回错误... _italic_致命错误:调用未定义的函数DateTime()在第63行_italic_ ...这是... '$ currentDate = DateTime();' – Amesey 2013-02-23 14:57:49

+1

对不起,你需要指定'new DateTime()' - 再试一次 – hohner 2013-02-23 14:59:21

+0

这很有效,谢谢,但不幸的是我现在才意识到,因为我显示的是下一个5事件,因为所有正在调用的事件已经过期,所以循环实际上不显示事件。我想我需要过滤比今天更早的帖子,而不是向旧帖子添加一个类,否则新帖子不会显示。 – Amesey 2013-02-23 15:04:03

相关问题