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

2022-08-30 16:56:27

我有一个侧边栏,我想在其中显示最新的帖子。现在,它显示了标题,日期和摘录。日期显示我想摆脱的时间。我用这个显示日期:$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并且没有时间。提前致谢。


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

这将按照您想要的方式设置其格式。只需将循环中的替换为它即可。$recent['post_date']


答案 2

虽然Syfaro的答案是正确的,但最佳做法是使用WordPress自己的功能来实现这一点。

get_the_date

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

另外,不要忘记逃避输出 - 检查esc_htmlesc_html_e


推荐