PHP:如果“if”语句为true,则执行while循环

时间:2013-08-29 04:34:13

标签: php wordpress

我试图仅显示当前日期或将来的那些事件。为了显示事件我有这样的代码:

<?php 
    $wp_query = new WP_Query();
    global $more;
    // set $more to 0 in order to only get the first part of the post
    $more = 0; 
    $wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );

    while ( ($wp_query->have_posts()) ): $wp_query->the_post();
        get_template_part( 'content', 'event' );
    endwhile;
?>

我试图添加&#34; IF语句&#34;将事件开始日期与当前日期进行比较,但它似乎不起作用。我尝试了很多版本,但我觉得应该可以使用这个版本:

 $wp_query->query( array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC') );
 $OstartDate = get_post_meta($post->ID, '_event_start', TRUE);
 $today = date('d.m.Y');
 while ( ($wp_query->have_posts()) && ($OstarDate < $today) ): 
      $wp_query->the_post();

2 个答案:

答案 0 :(得分:0)

从你应该尝试的second one

$OstartDate=get_post_meta($post->ID,'_event_start',TRUE);//check it will return a number
if(time() > $OstartDate)
{
   while ($wp_query->have_posts()):
      $wp_query->the_post();
   .....
}

a7-simple-events可能对您有帮助。

答案 1 :(得分:0)

您可以使用meta_query

将帖子与postmeta进行比较
$today = date('d.m.Y');
 $args=array( 'post_type' => 'events', 'posts_per_page' => 7, 'paged' => $paged, 'orderby' => 'menu_order', 'order' => 'ASC', 'meta_query'            => array(
        array(
            'key' => '_event_start',
            'value' => $today,
            'type' => 'date',
            'compare' => '<'
        )
    )) ;
    $wp_query = new WP_Query( $args );

 while ( ($wp_query->have_posts())  ): 
      $wp_query->the_post();
      endwhile;