wordpress自定义查询事件日历

时间:2013-10-21 19:44:50

标签: wordpress events calendar

我在'u5'事件类别中有2个事件。结果显示如下:

U5 vs Eagan
没有事件

如何在没有“无事件”输出的情况下显示事件?

date_default_timezone_set( 'America/Chicago' );
$today = date('j M Y');
$tomorrow = date('j M Y', strtotime('+1 days'));
$third = date('j M Y', strtotime('+2 days'));
$fourth = date('j M Y', strtotime('+3 days'));
$end_date = date('j M Y', strtotime('+4 days'));

global $post;
$upcoming = tribe_get_events(array('start_date'=>$today,'end_date'=>$end_date,'tribe_events_cat'=>'u5'));

if (count($upcoming) == 0) {
    echo '<li>No Events</li>';
} else {
    foreach($upcoming as $post) {
        setup_postdata($post);
        $start = tribe_get_start_date(null, false, 'j M Y');

        if($start == $tomorrow) {
            echo the_title();
        } else {
            echo 'no events';
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我已经将其重写为一个函数,使其可重用,并为给定的示例生成类似的输出。传递开始日期和天数,根据需要扩展其他组的数组。

function list_events( $start_date , $days ){
    date_default_timezone_set( 'America/Chicago' );
    global $post;
    $column_classes = array( 'first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh' );
    $start = strtotime( $start_date );
    for ( $i=0; $i<$days; $i++ ){
        $day = $start+86400*$i;
        $day_f=date( 'Y-m-d', $day );
        echo '<li class="'.$column_classes[$i].'">';
        echo '<span class="title">'.date('M j', $day).'</span>';
        $upcoming = tribe_get_events( array( 'eventDisplay'=>'all',  'start_date'=>$day_f,'end_date'=>$day_f.' 23:59:59' ) );
        if ( count( $upcoming ) == 0 ){
            echo '<span>No Events</span>';
        }else{
        foreach ( $upcoming as $post ){
            setup_postdata($post);
            echo '<span><a href="'.get_permalink().'">'.get_the_title().'</a></span>';
            }
        }
        echo '</li>';
    }
    wp_reset_query();
}