問題描述
兩個函式都返回日期和時間。他們有什麼區別?你有什麼例子嗎?謝謝。
最佳解決方案
他們非常相似,但有一些細微差別:
function get_the_date( $d = '' ) {
global $post;
$the_date = '';
if ( '' == $d )
$the_date .= mysql2date(get_option('date_format'), $post->post_date);
else
$the_date .= mysql2date($d, $post->post_date);
return apply_filters('get_the_date', $the_date, $d);
}
function get_the_time( $d = '', $post = null ) {
$post = get_post($post);
if ( '' == $d )
$the_time = get_post_time(get_option('time_format'), false, $post, true);
else
$the_time = get_post_time($d, false, $post, true);
return apply_filters('get_the_time', $the_time, $d, $post);
}
-
get_the_date()始終適用於當前全域性$post,get_the_time()允許您指定 post 作為引數。 -
它們預設為不同的格式,分別儲存在
date_format和time_format選項中。 -
它們分別透過不同的過濾器
get_the_date和get_the_time加上低階別的get_post_time。
次佳解決方案
the_date()模板標籤僅在每次出現時輸出一次釋出日期; 因此,如果兩個或更多個帖子具有相同的釋出日期,那麼該日期僅在迴圈中首次出現時輸出。 the_time()模板標籤根據正常輸出釋出時間 (使用任何有效的日期/時間字串) 。
然而,get_the_date()和 get_the_time()模板標籤基本相同。它們用於返回 the_date()和 the_time()的相應值。 As per the Codex:
Unlike
the_date()this tag will always return the date. Modify output with ‘get_the_date’ filter.
所以,區別在於 get_the_*()模板標籤本身,而在 the_*()模板標籤中使用它們。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。