get_the_modified_time()
云策文档标注
概述
get_the_modified_time() 函数用于获取文章最后修改的时间,支持自定义格式和指定文章,返回格式化字符串、Unix 时间戳或失败时返回 false。
关键要点
- 参数 $format 可选,接受 'G'、'U' 或 PHP 日期格式,默认使用 'time_format' 选项
- 参数 $post 可选,接受文章 ID 或 WP_Post 对象,默认当前文章
- 返回值类型为 string|int|false,成功时返回格式化时间或时间戳,失败返回 false
- 函数内部调用 get_post_modified_time() 并应用过滤器 get_the_modified_time
代码示例
function get_the_modified_time( $format = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
// For backward compatibility, failures go through the filter below.
$the_time = false;
} else {
$_format = ! empty( $format ) ? $format : get_option( 'time_format' );
$the_time = get_post_modified_time( $_format, false, $post, true );
}
/**
* Filters the localized time a post was last modified.
*
* @since 2.0.0
* @since 4.6.0 Added the `$post` parameter.
*
* @param string|int|false $the_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.
* @param WP_Post|null $post WP_Post object or null if no post is found.
*/
return apply_filters( 'get_the_modified_time', $the_time, $format, $post );
}注意事项
- 从 4.6.0 版本开始,添加了 $post 参数,允许指定文章
- 函数依赖 get_post_modified_time()、get_option() 和 get_post() 等核心函数
- 可通过过滤器 get_the_modified_time 自定义输出
原文内容
Retrieves the time at which the post was last modified.
Parameters
Source
function get_the_modified_time( $format = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
// For backward compatibility, failures go through the filter below.
$the_time = false;
} else {
$_format = ! empty( $format ) ? $format : get_option( 'time_format' );
$the_time = get_post_modified_time( $_format, false, $post, true );
}
/**
* Filters the localized time a post was last modified.
*
* @since 2.0.0
* @since 4.6.0 Added the `$post` parameter.
*
* @param string|int|false $the_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.
* @param WP_Post|null $post WP_Post object or null if no post is found.
*/
return apply_filters( 'get_the_modified_time', $the_time, $format, $post );
}
Hooks
- apply_filters( ‘get_the_modified_time’, string|int|false $the_time, string $format, WP_Post|null $post )
-
Filters the localized time a post was last modified.