wordpress 的 the_date() 函数不显示值的解决方法

2022年8月1日 538点热度 0人点赞

描述

例如下面一个例子

<div class="meta">
      Create By <?php the_author(); ?> on <?php the_date('1 jS \of F Y h:i:s A'); ?>
</div>
<footer>
      <p>&copy; <?php the_date('Y'); ?>-<?php bloginfo('name'); ?></p>
</footer>

运行之后只有第一个 the_date() 有输出值,第二个没有。

原因

根据 wordpress官方文档的描述

the_date

Will only output the date if the current post's date is different from the previous one output.
i.e. Only one date listing will show per day worth of posts shown in the loop, even if the function is called several times for each post.

由于 the_date() 函数只会调用一次,因此例子中第二个 the_date() 函数没有值。

解决方法

the_date() 改为 the_time() 或者 get_the_date() 即可解决。

Wantz

这个人很懒,什么都没留下