函数文档

the_time()

💡 云策文档标注

概述

the_time() 函数用于在 WordPress 中显示文章的发布时间。它接受一个可选的格式参数,并支持通过 'the_time' 过滤器进行自定义。

关键要点

  • 函数功能:输出文章的格式化时间,基于 get_the_time() 获取数据。
  • 参数:$format(可选),指定时间格式,接受 'G'、'U' 或 PHP 日期格式,默认为 'time_format' 选项。
  • Hook:使用 apply_filters('the_time', ...) 允许开发者过滤显示的时间。
  • 相关函数:get_the_time() 用于检索时间,apply_filters() 用于调用过滤器回调。
  • 版本历史:自 WordPress 0.71 版本引入。

代码示例

function the_time( $format = '' ) {
    echo apply_filters( 'the_time', get_the_time( $format ), $format );
}

注意事项

  • 用户贡献笔记提供了多种使用示例,如显示日期('F j, Y')、时间格式('g:i a' 或 'G:i')以及组合日期和时间。
  • 完整的日期和时间格式代码可参考官方文档。

📄 原文内容

Displays the time of the post.

Parameters

$formatstringoptional
Format to use for retrieving the time the post was written. Accepts 'G', 'U', or PHP date format.
Defaults to the 'time_format' option.

Source

function the_time( $format = '' ) {
	/**
	 * Filters the time of the post, for display.
	 *
	 * @since 0.71
	 *
	 * @param string $get_the_time The formatted time.
	 * @param string $format       Format to use for retrieving the time the post
	 *                             was written. Accepts 'G', 'U', or PHP date format.
	 */
	echo apply_filters( 'the_time', get_the_time( $format ), $format );
}

Hooks

apply_filters( ‘the_time’, string $get_the_time, string $format )

Filters the time of the post, for display.

Changelog

Version Description
0.71 Introduced.

User Contributed Notes