the_modified_time()
云策文档标注
概述
the_modified_time() 是一个 WordPress 模板标签,用于直接输出文章的最后修改时间。它接受一个可选的格式参数,并支持通过 Hook 进行过滤。
关键要点
- 函数用于显示文章的最后修改时间,基于当前全局 $post 对象。
- 参数 $format 可选,接受 'G'、'U' 或 PHP 日期格式字符串,默认使用 'time_format' 选项。
- 内部调用 get_the_modified_time() 获取时间,并通过 apply_filters('the_modified_time', ...) 应用过滤。
- 建议在需要同时显示修改时间和创建时间时,使用条件语句避免重复显示相同时间。
- 函数自 WordPress 2.0.0 版本引入,位于 wp-includes/general-template.php 文件中。
代码示例
if ( get_the_modified_time() != get_the_time() ) {
// 显示修改时间,避免与创建时间重复
the_modified_time('F j, Y');
}注意事项
- 输出是直接回显的,如果需要返回值,应使用 get_the_modified_time()。
- 格式参数遵循 PHP date() 函数规则,例如 'g:i a' 表示 12 小时制时间。
- 在主题或插件开发中,可通过 'the_modified_time' 过滤器自定义输出。
原文内容
Displays the time at which the post was last modified.
Parameters
$formatstringoptional-
Format to use for retrieving the time the post was modified. Accepts
'G','U', or PHP date format.
Defaults to the'time_format'option.
Source
function the_modified_time( $format = '' ) {
/**
* Filters the localized time a post was last modified, for display.
*
* @since 2.0.0
*
* @param string|false $get_the_modified_time The formatted time or false if no post is found.
* @param string $format Format to use for retrieving the time the post
* was modified. Accepts 'G', 'U', or PHP date format.
*/
echo apply_filters( 'the_modified_time', get_the_modified_time( $format ), $format );
}
Hooks
- apply_filters( ‘the_modified_time’, string|false $get_the_modified_time, string $format )
-
Filters the localized time a post was last modified, for display.
Changelog
| Version | Description |
|---|---|
| 2.0.0 | Introduced. |
Skip to note 6 content
Codex
Default Usage
Displays the time (date) the post was last modified, using the Default date format setting (e.g. F j, Y) from Administration > Settings > General.
<p>Last modified: </p>Output is “Last modified: December 2, 2006”
Skip to note 7 content
Codex
Time in the 12-hour format (am/pm)
If a post was modified at 10:36pm, this example displays the time the post was last modified using the 12-hour format parameter string ‘g:i a’.
<p>Time last modified: </p>Output is “Time last modified: 10:36 pm”
Skip to note 8 content
Codex
Time in the 24-hour format
If a post was modified at 10:36pm, this example displays the time the post was last modified using the 24-hour format parameter string ‘G:i’.
<p>Time last modified: </p>Output is “Time last modified: 22:36”
Skip to note 9 content
Codex
Date as Month Day, Year
Displays the last modified time and date in the date format ‘F j, Y’ (ex: December 2, 2006), which could be used to replace the tag the_modified_date() .
<div>Last modified: </div>Output is “Last modified: December 2, 2006”.
Skip to note 10 content
Codex
Date and Time
Displays the date and time.
<p>Modified: at </p>Output is “Modified: December 2, 2006 at 10:36 pm”.