簡體   English   中英

如何在 Wordpress 中格式化發布日期?

[英]How do I format the post date in Wordpress?

我有一個側邊欄,我想在其中顯示最新的帖子。 現在它顯示標題、日期和摘錄。 日期顯示我想擺脫的時間。 我用這個顯示日期:$recent["post_date"]

 <?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    echo '<li id="sidebar_text"><b>'.$recent["post_title"].'</b></li><li style="font-size:12px">'.$recent["post_date"].'</li><li><i style="font-size:15px">'.$recent["post_excerpt"].'</i><a href="'.get_permalink($recent["ID"]).'"> Read   More</a></li>';
     }
 ?>

它顯示的日期是這樣的:2013-08-11 18:29:04,我希望它像這樣 8-11-2013 而沒有時間。 提前致謝。

date('n-j-Y', strtotime($recent['post_date']));

這會按照您想要的方式對其進行格式化。 只需用它替換循環中的$recent['post_date']

雖然 Syfaro 的回答是正確的,但最佳做法是為此使用 WordPress 自己的功能。

獲取日期

這默認為 WordPress 管理設置(設置 -> 常規)中設置的格式,因此為將來的編輯提供了更易於訪問的解決方案 - 如果您在多個站點中滾動代碼,或者更重要的是如果您公開發布代碼,則特別有用。

另外,不要忘記轉義輸出- 查看esc_htmlesc_html_e

date_i18n('l d/m/Y \à\s g:i', strtotime($item['time']))

$recent["post_date"]替換$recent["post_date"] mysql2date('nj-Y', $recent['post_date'])

暫無
暫無

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

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