函数文档

the_modified_date()

💡 云策文档标注

概述

the_modified_date() 是一个 WordPress 模板标签,用于显示文章最后修改的日期。它必须在 The Loop 中使用,并支持自定义日期格式、前后缀以及控制输出方式。

关键要点

  • 显示文章最后修改日期,若未修改则显示创建日期
  • 必须在 The Loop 中使用
  • 参数包括 $format(日期格式,默认使用后台设置的默认日期格式)、$before(日期前输出)、$after(日期后输出)和 $display(是否直接输出,默认为 true)
  • 返回类型为 string 或 void,取决于 $display 参数
  • 使用 get_the_modified_date() 来获取日期值
  • 可通过 apply_filters('the_modified_date', ...) 钩子过滤输出

代码示例

// 默认使用后台设置的日期格式显示修改日期
the_modified_date();

// 自定义日期格式,例如显示为“December 2, 2006”
the_modified_date('F j, Y');

// 添加前后缀并返回字符串而不直接输出
$date = the_modified_date('', 'Last modified: ', '', false);

注意事项

  • 如果未指定 $format 参数,将使用 WordPress 后台“设置”>“常规”中的“默认日期格式”设置
  • 此标签与 the_modified_time() 功能类似,但专注于日期显示
  • 在输出包含 HTML 标签的日期格式时,需使用反斜杠转义特殊字符,例如在 superscript 或 subscript 中使用

📄 原文内容

Displays the date on which the post was last modified.

Parameters

$formatstringoptional
PHP date format. Defaults to the 'date_format' option.
$beforestringoptional
Output before the date. Default empty.
$afterstringoptional
Output after the date. Default empty.
$displaybooloptional
Whether to echo the date or return it.

Default:true

Return

string|void String if retrieving.

More Information

This tag works just like the_modified_time(), which also displays the time/date a post was last modified. This tag must be used within The Loop. If no format parameter is specified, the Default date format (please note that says Date format) setting from Administration > Settings > General is used for the display format.

If the post or page is not yet modified, the modified date is the same as the creation date.

Use get_the_modified_date() to retrieve the value.

Source

function the_modified_date( $format = '', $before = '', $after = '', $display = true ) {
	$the_modified_date = $before . get_the_modified_date( $format ) . $after;

	/**
	 * Filters the date a post was last modified, for display.
	 *
	 * @since 2.1.0
	 *
	 * @param string $the_modified_date The last modified date.
	 * @param string $format            PHP date format.
	 * @param string $before            HTML output before the date.
	 * @param string $after             HTML output after the date.
	 */
	$the_modified_date = apply_filters( 'the_modified_date', $the_modified_date, $format, $before, $after );

	if ( $display ) {
		echo $the_modified_date;
	} else {
		return $the_modified_date;
	}
}

Hooks

apply_filters( ‘the_modified_date’, string $the_modified_date, string $format, string $before, string $after )

Filters the date a post was last modified, for display.

Changelog

Version Description
2.1.0 Introduced.

User Contributed Notes

  1. Skip to note 8 content

    Date with superscript or subscript number suffixes
    Displays the date with a superscript or subscript st, nd, rd or th after the day. Because characters from the alphabet are used to represent the date format types, each of the HTML tag characters need to be escaped using a back slash. Superscript HTML tag is and subscript is .

    <p>S</sup> M Y') ); ?></p>

    Modified: 2nd Dec 2006