函数文档

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.

More Information

If you want to display both the modified time and the creation time, you may want to use an if statement to avoid showing the same time/date twice.
For example:

if ( get_the_modified_time() != get_the_time()) )

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.

User Contributed Notes