問題描述

兩個函數都返回日期和時間。他們有什麼區別?你有什麼例子嗎?謝謝。

最佳解決方案

他們非常相似,但有一些細微差別:

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);
}
  1. get_the_date()始終適用於當前全局 $postget_the_time()允許您指定 post 作為參數。

  2. 它們默認為不同的格式,分別存儲在 date_formattime_format 選項中。

  3. 它們分別通過不同的過濾器 get_the_dateget_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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。