get_the_modified_date()
云策文档标注
概述
get_the_modified_date() 函数用于获取文章的最后修改日期,支持自定义日期格式和指定文章。它返回格式化后的日期字符串、时间戳或失败时返回 false。
关键要点
- 函数参数:$format(可选,PHP 日期格式,默认使用 'date_format' 选项)和 $post(可选,文章 ID 或 WP_Post 对象,默认当前文章)。
- 返回值:成功时返回日期字符串或时间戳,失败时返回 false。
- 内部实现:通过 get_post_modified_time() 获取修改时间,并应用 'get_the_modified_date' 过滤器。
- 相关函数:包括 get_post_modified_time()、apply_filters()、get_option() 和 get_post()。
- 变更记录:从 2.1.0 版本引入,4.6.0 版本添加了 $post 参数。
代码示例
function get_the_modified_date( $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( 'date_format' );
$the_time = get_post_modified_time( $_format, false, $post, true );
}
/**
* Filters the date a post was last modified.
*
* @since 2.1.0
* @since 4.6.0 Added the `$post` parameter.
*
* @param string|int|false $the_time The formatted date or false if no post is found.
* @param string $format PHP date format.
* @param WP_Post|null $post WP_Post object or null if no post is found.
*/
return apply_filters( 'get_the_modified_date', $the_time, $format, $post );
}注意事项
- 函数依赖于 get_post_modified_time() 来获取实际修改时间,确保文章数据可用。
- 使用 'get_the_modified_date' 过滤器可以自定义输出,例如修改日期格式或添加额外逻辑。
- 在无文章或失败时返回 false,调用时应检查返回值以避免错误。
原文内容
Retrieves the date on which the post was last modified.
Parameters
Source
function get_the_modified_date( $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( 'date_format' );
$the_time = get_post_modified_time( $_format, false, $post, true );
}
/**
* Filters the date a post was last modified.
*
* @since 2.1.0
* @since 4.6.0 Added the `$post` parameter.
*
* @param string|int|false $the_time The formatted date or false if no post is found.
* @param string $format PHP date format.
* @param WP_Post|null $post WP_Post object or null if no post is found.
*/
return apply_filters( 'get_the_modified_date', $the_time, $format, $post );
}
Hooks
- apply_filters( ‘get_the_modified_date’, string|int|false $the_time, string $format, WP_Post|null $post )
-
Filters the date a post was last modified.