腾讯云服务器优惠活动华纳云香港服务器

WordPress最后修改时间函数get_the_modified_time()的介绍及使用

WordPress主题推荐

我们使用 WordPress 编辑文章有一个发布时间,只要修改过就会出现一个修改时间,平时大多数都是使用文章的发布时间,但是有时候还需要将发布时间与修改时间作对比然后判断是展示发布时间还是修改时间或其他,这个时候就需要用到 get_the_modified_time()函数了,这个函数就是今天的重点内容。

WordPress最后修改时间函数get_the_modified_time()的介绍及使用-第1张-boke112百科(boke112.com)

get_the_modified_time()函数介绍

检索(获取)当前文章最后修改的时间。

get_the_modified_time( string $format = '', int|WP_Post $post = null )

参数:

$format(string):(可选)用于检索撰写文章修改时间的格式。“G”,“U”或 PHP 日期格式默认为 time_format 选项中指定的值。默认值:”

$post(int|WP_Post):(可选)WP_Post 对象或 ID,默认为当前文章。默认值:null

返回:

(string|int|false) 格式化的日期字符串或 Unix 时间戳,如果$format 是’U’或’G’。错误的失败。

函数所在文件:wp-includes/general-template.php

function get_the_modified_time( $format = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
$the_time = false;
} else {
$_format = ! empty( $format ) ? $format : get_option( 'time_format' );

$the_time = get_post_modified_time( $_format, false, $post, true );
}
return apply_filters( 'get_the_modified_time', $the_time, $format, $post );
}

get_the_modified_time()函数使用示例

示例:使用 WordPress 默认格式返回当前帖子的最后修改时间,并使用 PHP echo 命令显示它。

<?php echo get_the_modified_time(); ?>

示例:以 WordPress 默认格式返回 ID 为$ post-> ID 的帖子的最后修改时间。

<?php echo get_the_modified_time( '', $post->ID ); ?>

示例:将以秒为单位的当前帖子的最后修改时间(自 1970 年 1 月 1 日以来,被称为 Unix Epoch)分配给变量$ u_time。

<?php $local_timestamp = get_the_modified_time( 'U' ); ?>

示例:通过判断文章的最后修改时间来给文章添加某些提示,如『给 WordPress 长期无更新旧文章增加自定义提示内容』和『WordPress 如何实现多久时间后在文章最后添加更新时间?』。

以上内容整理自@WordPress – get_the_modified_time()

本文地址:https://boke112.com/post/7371.html

赞 (0) 打赏
版权声明:本文为原创文章,版权归 boke112百科 所有,欢迎分享本文,转载请保留出处!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
香港云主机55元/年
wu