2013-12-22 146 views
0

我正在學習如何製作一個WordPress插件。我做了幾個簡單的插件,但沒有像這個那麼複雜。這是一個活動日曆。函數「nc_get_start_date()」的var_dump在頁面上輸出錯誤的日期。PHP函數返回錯誤日期

var_dump(nc_get_start_date());

字符串(32) 「1970-01-01,1970-01-01,1970-01-01」

這是什麼功能應該在現實

返回
23-12-2013, 25-12-2013, 26-12-2013 

在插件文件夾的function.php中。這是代碼

/* Query to get the events post from the database */ 
    function get_nc_events(){ 
     global $post; 
     $query = new WP_Query( 
      array( 
       'post_type' => 'events', 
       'post_status' => 'publish', 
       'posts_per_page' => -1, 
       'order' => 'ASC' 
      ) 
     ); 
     return $query; 
    } 

/* Get the start date from the above function */ 
    function nc_get_start_date(){ 
     $query = get_nc_events(); 
     while ($query->have_posts()) : $query->the_post(); 
      $nc_event_id = $post->ID; 
      $wnc_start_date = get_post_meta($nc_event_id, 'wnc_start_date'); 
      $wnc_start_date = $wnc_start_date[0]; 
      $wnc_start_date = date("Y-m-d", strtotime($wnc_start_date)); 
      $wnc_start_date_array .= "$wnc_start_date,"; 
     endwhile; 
     return rtrim($wnc_start_date_array, ","); 
    } 

當我寫在頁面caledar.php的代碼沒有它呈現的一切prefectly功能。

$query = get_nc_events(); 
while ($query->have_posts()) : $query->the_post(); 
    $nc_event_id = $post->ID; 
    $wnc_start_date = get_post_meta($nc_event_id, 'wnc_start_date'); 
    echo $wnc_start_date = $wnc_start_date[0] . "<br/>"; 
endwhile; 
+0

是什麼get_post_meta($ nc_event_id, 'wnc_start_date')' –

+0

的'值從'get_post_meta'我得到** 23-12-2013,25-12-2013,26-12-2013 ** – Johny

+0

這unix時代的日期,這導致我相信$ wnc_start_date [0]是0 – Zarathuztra

回答

1

問題解決。感謝大家。問題出在這個功能

/* Get the start date from the above function */ 
    function nc_get_start_date(){ 
     global $post; 
     $query = get_nc_events(); 
     while ($query->have_posts()) : $query->the_post(); 
      $nc_event_id = $post->ID; 
      $wnc_start_date = get_post_meta($nc_event_id, 'wnc_start_date'); 
      $wnc_start_date = $wnc_start_date[0]; 
      $wnc_start_date = date("Y-m-d", strtotime($wnc_start_date)); 
      $wnc_start_date_array .= "$wnc_start_date,"; 
     endwhile; 
     return rtrim($wnc_start_date_array, ","); 
    } 

我沒有做global $ post;