php获取文章发布时间,WordPress获取文章发布时间函数get_the_time()的介绍及使用

我们使用 WordPress 编辑文章的时候都会有一个发布时间,正常情况下都会在文章页或列表页中显示文章对应的发布时间,有时候还需要将发布时间与修改时间作对比然后判断是展示发布时间还是修改时间或其他,这个时候就需要用到 get_the_time()函数了,这个函数就是今天的重点内容。

8dc7c9d4711b3ed93bfe1671db8c558a.png

get_the_time()函数介绍

检索(获取)当前文章的发布时间。

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

参数:

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

$post(int|WP_Post):(可选)WP_Post 对象或 ID,默认为全局$post 对象。默认值:null

返回:

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

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

function get_the_time( $format = '', $post = null ) {

$post = get_post( $post );

if ( ! $post ) {

return false;

}

$_format = ! empty( $format ) ? $format : get_option( 'time_format' );

$the_time = get_post_time( $_format, false, $post, true );

return apply_filters( 'get_the_time', $the_time, $format, $post );

}

get_the_time()函数使用示例

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

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

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

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

示例:如通过判断文章发布时间和最后修改时间来添加一些提示,如『WordPress 如何实现多久时间后在文章最后添加更新时间?』。

以上内容整理自@WordPress - get_the_time()

ad51e517755f8fd6a7ec83ced4ecfaf3.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值