2

WordPress文章或页面获取发布时间和修改时间

 2 years ago
source link: https://www.huhexian.com/22753.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

WordPress文章或页面获取发布时间和修改时间

青山 2022-01-1211:43:05评论600字

其实Wordpress想要获取当前文章或页面的发布时间和修改时间。其实很简单,因为 WordPress 已经提供了时间函数 the_time() 和 the_modified_time() ,我们只需在当前主题下的模板页需要显示时间的位置添加以下代码即可。

WordPress文章或页面获取发布时间和修改时间

获取发布时间:

  1. <?php the_time('Y年n月d日 H:i:s'); ?>

获取修改时间:

  1. <?php the_modified_time('Y年n月d日 H:i:s'); ?>

但是这并不是白天想要的效果,如果修改时间和发布时间一致,显示两个一样的时间就会显得很鸡肋。于是还得再做优化。

我们可以增加一个判断,如果当前文章或页面做过修改,那么就需要增加修改时间,反之则只显示发布时间。代码如下:

  1. <p>发布于<?php the_time('Y年n月d日 H:i:s'); ?>
  2. <?php if ((get_the_modified_time('Y')*365+get_the_modified_time('z')) >(get_the_time('Y')*365+get_the_time('z'))) { ?>
  3. <?php echo ',最后一次修改于' ?><?php the_modified_time('Y年n月d日 H:i:s'); ?>
  4. <?php } ?>
  5. </p>

利用if语句让修改时间与发布时间做对比,如果修改时间大于发布时间,那么就显示发布时间和修改时间,反之则只显示发布时间。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK