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

Mik*_*ike 15 php wordpress sidebar

我有一个侧边栏,我想显示最近的帖子.现在它显示标题,日期和摘录.日期显示我想要摆脱的时间.我用这个显示日期:$ 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>';
     }
 ?>
Run Code Online (Sandbox Code Playgroud)

它显示了这样的日期:2013-08-11 18:29:04我希望它像这样8-11-2013,没有时间.提前致谢.

ixc*_*chi 32

date('n-j-Y', strtotime($recent['post_date']));
Run Code Online (Sandbox Code Playgroud)

这可以按照您的方式进行格式化.只需用$recent['post_date']你的循环替换它.

  • 使用`get_option( 'date_format' )` 获取设置&gt; 常规&gt; 日期格式中定义的默认日期格式 (7认同)

Mar*_*ark 5

虽然Syfaro的答案是正确的,但最佳做法是使用WordPress自己的功能.

get_the_date

这默认为WordPress管理设置(设置 - >常规)中设置的格式,因此为将来的编辑提供了更易于访问的解决方案 - 如果您在多个站点中滚动代码,或者更重要的是如果您公开发布它,则特别有用.

另外,不要忘记转义输出 - 请查看esc_htmlesc_html_e


小智 5

date_i18n('l d/m/Y \\\xc3\xa0\\s g:i', strtotime($item['time']))\n
Run Code Online (Sandbox Code Playgroud)\n