0

我正在使用自定义查询在 Wordpress 中构建部落事件日历自定义轮播查询 (wP_Query)。我的事件查询运行良好,但我什至想显示没有安排事件的空日期。这是可能的还是有解决方法?任何帮助,将不胜感激!

我的查询代码:

$args = array(
   'post_status'=>'publish',
   'post_type'=>array(TribeEvents::POSTTYPE),
   'posts_per_page'=>10,
   'start_date'     => date( 'Y-m-d H:i:s', strtotime( '-1 week' ) ),
   'end_date'       => date( 'Y-m-d H:i:s', strtotime( '+1 month' ) ),
   //order by startdate from newest to oldest
   'meta_key'=>'_EventStartDate',
   'orderby'=>'_EventStartDate',
   'order'=>'ASC',
   //required in 3.x
   'eventDisplay'=>'custom', 
);

$get_posts = null;
$get_posts = new WP_Query();
$get_posts->query($args);

if($get_posts->have_posts()) {
   while($get_posts->have_posts()) : $get_posts->the_post();  

  // display results here from loop

  endwhile;
}

wp_reset_query();

更新:我发现这个脚本可能有效。然后我可以弄乱日期的格式。我相信我只是在考虑实际问题。一旦弄清楚这一点,我将提供一个工作代码。

$start_date = '2012-01-01'; /* I can change this to strtotime( '-1 week' ) */
$end_date = '2012-12-31'; /* and this to strtotime( '+1 month' ) but mess with the format in PHP Y-m-d */

$start    = new DateTime($start_date);
$end      = new DateTime($end_date);
$interval = new DateInterval('P1D'); // 1 day interval
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $day) {
    // Do stuff with each $day...
    echo $day->format('Y-m-d'), "\n";
}
4

0 回答 0