【问题标题】:Hide old event posts older than current date隐藏早于当前日期的旧活动帖子
【发布时间】:2016-07-20 19:22:52
【问题描述】:

我当前的循环显示 5 个即将举行的活动,但是当活动当天过去时,我无法让这些活动帖子不显示。这是我的代码...

<?

wp_reset_query();
query_posts(array('post_type' => 'events',
                  'showposts' => 5,
                  'meta_key'=>'event_date',  
                  'orderby' => 'meta_value', 
                  'order' => ASC));

while (have_posts()) : the_post(); 

?>

<li>
<?php $eventDate = DateTime::createFromFormat('Ymd', get_field('event_date')); ?>
<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;?>

...任何帮助将不胜感激

【问题讨论】:

标签: wordpress while-loop


【解决方案1】:

由于您的帖子有自定义字段,您可以执行以下操作:

<?php

 // Get the current date
 $current_date = date('M d, Y');
 $current_date = strtotime( $current_date );


 query_posts(array('post_type' => 'events',
              'showposts' => 5,
              'meta_key'=>'event_date',  
              'orderby' => 'meta_value', 
              'order' => ASC));

 while (have_posts()) : the_post(); 

   // Get the custom field
   $post_date = get('date');
   $post_date = strtotime( $post_date );


   // If older than current date, don't show it
   if( $post_date > $current_date ):

?>

  <li>
   <?php $eventDate = DateTime::createFromFormat('Ymd', get_field('event_date')); ?>
   <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
     endif;
 endwhile;
?>

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多