簡體   English   中英

WordPress: - get_the_date()函數返回錯誤的日期

[英]WordPress:- get_the_date() function returning a wrong date

這是一個Word Press / PHP問題(非常初學者,我猜)。 我正在嘗試使用以下代碼插入最新博客文章的鏈接,然后是發布日期。

<div class="latest_post">
    <ul><li><span class="recent_blog">LATEST POST</span><?php
    $args = array(
        'numberposts' => 1,
        'category' => 71,
        'post_status' => 'publish',
    );
    $recent_posts = wp_get_recent_posts( $args );
    foreach( $recent_posts as $recent ) {
        echo '<a href="' . get_permalink($recent["ID"]) . '"> <strong>' .   $recent["post_title"].'</strong></a>';
    }
    wp_reset_query();
    ?> (<?php echo get_the_date('Y/m/d'); ?>)</li></ul>
</div><!-- .latest_post -->

但是, <?php echo get_the_date('Y/m/d'); ?> <?php echo get_the_date('Y/m/d'); ?>返回錯誤的日期“(2015/04/23)”,我不知道它來自哪里。 它應該是(2017/01/02)。 誰能幫我找出它出錯的地方? 或者,任何其他方式來獲取正確的日期?

先感謝您!

實際上基於參考: - https://developer.wordpress.org/reference/functions/get_the_date/

(它檢索撰寫帖子的日期。)

因此要么在此函數中提供post id以獲取特定的Post日期

要么

如果您想要當前日期,那么您可以使用: -

<?php echo date('Y/m/d');?>

我想你必須這樣做: -

foreach( $recent_posts as $recent ) {
   echo '<a href="' . get_permalink($recent["ID"]) . '"> <strong>' .   $recent["post_title"].'</strong></a>';
   echo get_the_date('Y/m/d',$recent["ID"]);
}

你必須像上面那樣使用它。

<?php echo get_the_date( $format, $post_id ); ?>

$format
(字符串)(可選)PHP日期格式。 默認值:date_format選項(“設置”>“常規”面板上的“日期格式”)

$post_id
(整數)(可選)您要獲取的帖子的ID。 默認情況下,將獲取當前帖子。 默認值:null

請使用postID嘗試此操作

<?php $pfx_date = get_the_date( $format, $post_id ); ?>

參考以下鏈接:

https://codex.wordpress.org/Function_Reference/get_the_date

https://codex.wordpress.org/Function_Reference/the_date

它看起來,基於文檔( https://codex.wordpress.org/Function_Reference/get_the_date

“檢索當前 $ post的日期”,以便日期:(2015/04/23)可能指的是您正在創建所在帖子鏈接的活動頁面。

如果您能夠獲得帖子ID(新帖子),您將能夠獲得正確的日期: - get_the_date( $format, $post_id )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM