如何获得每个帖子的日期?
2 个回答
- 投票数
-
- 2013-03-11
过去几次更改对我有用,我几次遇到相同的问题:
while (have_posts()) : the_post(); //some html <li class="icon-date"><?php echo get_the_date( 'Y-m-d' ); ?></li> <li class="icon-time"><?php the_time( 'H:i:s' ); ?></li>
使用
get_the_date()
代替get_the_date()
.
唯一需要注意的是,必须回显get_the_date()
返回的值.查看"法典"页面,其中有关于
the_date()
.在同一天下发布的页面上有多个帖子时,the_date()仅显示第一个帖子的日期(即the_date()的第一个实例).要为同一天发布的帖子重复日期,您应该使用模板标签the_time()或get_the_date()(从3.0开始),并带有日期特定的格式字符串.
此外,如果要控制在Admin中返回
get_the_date()
的格式,则可以使用get_option('date_format')
.这样,如果您在管理员中更改日期格式,这些更改也将在您的代码中进行.while (have_posts()) : the_post(); //some html <li class="icon-date"><?php echo get_the_date( get_option('date_format') ); ?></li> <li class="icon-time"><?php the_time( 'H:i:s' ); ?></li>
I ran into the same problem several times, following changes worked for me in the past:
while (have_posts()) : the_post(); //some html <li class="icon-date"><?php echo get_the_date( 'Y-m-d' ); ?></li> <li class="icon-time"><?php the_time( 'H:i:s' ); ?></li>
Instead of
the_date()
, useget_the_date()
.
The only thing to be aware of, is that values returned byget_the_date()
have to be echoed.Looking at the Codex page there is a special note about
the_date()
.When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date() (since 3.0) with a date-specific format string.
Also, If you want to control the format in wich
get_the_date()
is returned in Admin, you can useget_option('date_format')
. This way if you change the date format in the Admin, these changes will me made in your code too.while (have_posts()) : the_post(); //some html <li class="icon-date"><?php echo get_the_date( get_option('date_format') ); ?></li> <li class="icon-time"><?php the_time( 'H:i:s' ); ?></li>
-
- 2013-03-11
在同一天的下发布的页面上有多个帖子时,the_date()仅显示第一篇帖子的日期(即the_date()的第一个实例).要为同一天发布的帖子重复日期,您应该使用模板标签the_time()或get_the_date()(从3.0开始),并带有日期专用格式字符串. 用于添加在管理界面中设置的日期.
有关更多信息,请访问此页面.
因此,根据wordpress Codex参考,正确的代码如下:
while (have_posts()) : the_post(); //some html <li class="icon-date"><?php echo get_the_date('Y-m-d');?></li> <li class="icon-time"><?php the_time('H:i:s');?></li>
When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date() (since 3.0) with a date-specific format string. Use to add the date set in the admin interface.
For more information visit this page.
So according to the wordpress codex reference the correct code will be as following :
while (have_posts()) : the_post(); //some html <li class="icon-date"><?php echo get_the_date('Y-m-d');?></li> <li class="icon-time"><?php the_time('H:i:s');?></li>
我正在使用以下方法获取每个帖子的日期:
但是,我只知道第一篇文章的发布日期,为什么?