php 如何在 Wordpress 中格式化发布日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18175498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I format the post date in Wordpress?
提问by Mike
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. Thanks in advance.
它显示的日期是这样的:2013-08-11 18:29:04,我希望它像这样 8-11-2013 而没有时间。提前致谢。
回答by ixchi
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']
循环中的 。
回答by Mark
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_htmland esc_html_e
另外,不要忘记转义输出- 查看esc_html和esc_html_e
回答by Yamil Duba
Replace $recent["post_date"]
with mysql2date('n-j-Y', $recent['post_date'])
.
替换$recent["post_date"]
为mysql2date('n-j-Y', $recent['post_date'])
。
回答by Joao Paulo Pinheiro
date_i18n('l d/m/Y \à\s g:i', strtotime($item['time']))