the_modified_date()
云策文档标注
概述
the_modified_date() 是一个 WordPress 模板标签,用于显示文章最后修改的日期。它必须在 The Loop 中使用,并支持自定义日期格式、前后缀以及控制输出方式。
关键要点
- 显示文章最后修改日期,若未修改则显示创建日期
- 必须在 The Loop 中使用
- 参数包括 $format(日期格式,默认使用后台设置的默认日期格式)、$before(日期前输出)、$after(日期后输出)和 $display(是否直接输出,默认为 true)
- 返回类型为 string 或 void,取决于 $display 参数
- 使用 get_the_modified_date() 来获取日期值
- 可通过 apply_filters('the_modified_date', ...) 钩子过滤输出
代码示例
// 默认使用后台设置的日期格式显示修改日期
the_modified_date();
// 自定义日期格式,例如显示为“December 2, 2006”
the_modified_date('F j, Y');
// 添加前后缀并返回字符串而不直接输出
$date = the_modified_date('', 'Last modified: ', '', false);注意事项
- 如果未指定 $format 参数,将使用 WordPress 后台“设置”>“常规”中的“默认日期格式”设置
- 此标签与 the_modified_time() 功能类似,但专注于日期显示
- 在输出包含 HTML 标签的日期格式时,需使用反斜杠转义特殊字符,例如在 superscript 或 subscript 中使用
原文内容
Displays the date on which the post was last modified.
Parameters
$formatstringoptional-
PHP date format. Defaults to the
'date_format'option. $beforestringoptional-
Output before the date. Default empty.
$afterstringoptional-
Output after the date. Default empty.
$displaybooloptional-
Whether to echo the date or return it.
Default:
true
Source
function the_modified_date( $format = '', $before = '', $after = '', $display = true ) {
$the_modified_date = $before . get_the_modified_date( $format ) . $after;
/**
* Filters the date a post was last modified, for display.
*
* @since 2.1.0
*
* @param string $the_modified_date The last modified date.
* @param string $format PHP date format.
* @param string $before HTML output before the date.
* @param string $after HTML output after the date.
*/
$the_modified_date = apply_filters( 'the_modified_date', $the_modified_date, $format, $before, $after );
if ( $display ) {
echo $the_modified_date;
} else {
return $the_modified_date;
}
}
Hooks
- apply_filters( ‘the_modified_date’, string $the_modified_date, string $format, string $before, string $after )
-
Filters the date a post was last modified, for display.
Changelog
| Version | Description |
|---|---|
| 2.1.0 | Introduced. |
Skip to note 5 content
Codex
Default Usage
Displays the date the post was last modified, using the Default date format setting (e.g. F j, Y) from Administration > Settings > General.
<p></p>Last modified: December 2, 2006Skip to note 6 content
Codex
Date as Month Day, Year
Displays the last modified date in the date format ‘F j, Y’ (ex: December 2, 2006).
<div></div>Last modified: December 2, 2006Skip to note 7 content
Codex
Date and Time
Displays the date and time.
<p></p>Modified: December 2, 2006 at 10:36 pmSkip to note 8 content
Codex
Date with superscript or subscript number suffixes
Displays the date with a superscript or subscript st, nd, rd or th after the day. Because characters from the alphabet are used to represent the date format types, each of the HTML tag characters need to be escaped using a back slash. Superscript HTML tag is and subscript is .
<p>S</sup> M Y') ); ?></p>Modified: 2nd Dec 2006