[英]How do I format the post date in Wordpress?
I have a sidebar where I want to show the most recent posts.我有一个侧边栏,我想在其中显示最新的帖子。 Right now it shows the title, date, and an excerpt.现在它显示标题、日期和摘录。 The date shows the time which I want to get rid of.日期显示我想摆脱的时间。 I show the date using this: $recent["post_date"]我用这个显示日期:$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>';
}
?>
It shows the date like this: 2013-08-11 18:29:04 and I would like it like this 8-11-2013 and without the time.它显示的日期是这样的:2013-08-11 18:29:04,我希望它像这样 8-11-2013 而没有时间。 Thanks in advance.提前致谢。
date('n-j-Y', strtotime($recent['post_date']));
This formats it the way you want.这会按照您想要的方式对其进行格式化。 Just replace the $recent['post_date']
in your loop with that.只需用它替换循环中的$recent['post_date']
。
Whilst Syfaro's answer is correct, best practice is to use WordPress's own function for this.虽然 Syfaro 的回答是正确的,但最佳做法是为此使用 WordPress 自己的功能。
This defaults to the format set in the WordPress admin settings (Settings -> General), so gives a more accessible solution for future editing - particularly useful if you roll your code in multiple sites, or more importantly if you release it publicly.这默认为 WordPress 管理设置(设置 -> 常规)中设置的格式,因此为将来的编辑提供了更易于访问的解决方案 - 如果您在多个站点中滚动代码,或者更重要的是如果您公开发布代码,则特别有用。
Also, don't forget to escape output - check out esc_html and esc_html_e另外,不要忘记转义输出- 查看esc_html和esc_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.