返回介绍

the_date()

发布于 2017-09-11 10:33:55 字数 3917 浏览 1278 评论 0 收藏 0

the_date( string $d = '',  string $before = '',  string $after = '',  bool $echo = true )

Display or Retrieve the date the current post was written (once per date)


description

Will only output the date if the current post’s date is different from the previous one output.

i.e. Only one date listing will show per day worth of posts shown in the loop, even if the function is called several times for each post.

HTML output can be filtered with ‘the_date’. Date string output can be filtered with ‘get_the_date’.


参数

$d

(string) (Optional) PHP date format defaults to the date_format option if not specified.

Default value: ''

$before

(string) (Optional) Output before the date.

Default value: ''

$after

(string) (Optional) Output after the date.

Default value: ''

$echo

(bool) (Optional) default is display. Whether to echo the date or return it.

Default value: true


返回值

(string|void) String if retrieving.


源代码

File: wp-includes/general-template.php

function the_date( $d = '', $before = '', $after = '', $echo = true ) {
	global $currentday, $previousday;

	if ( is_new_day() ) {
		$the_date = $before . get_the_date( $d ) . $after;
		$previousday = $currentday;

		/**
		 * Filters the date a post was published for display.
		 *
		 * @since 0.71
		 *
		 * @param string $the_date The formatted date string.
		 * @param string $d        PHP date format. Defaults to 'date_format' option
		 *                         if not specified.
		 * @param string $before   HTML output before the date.
		 * @param string $after    HTML output after the date.
		 */
		$the_date = apply_filters( 'the_date', $the_date, $d, $before, $after );

		if ( $echo )
			echo $the_date;
		else
			return $the_date;
	}
}

更新日志

Versiondescription
0.71Introduced.

相关函数

Uses

  • wp-includes/general-template.php: get_the_date()
  • wp-includes/general-template.php: the_date
  • wp-includes/functions.php: is_new_day()
  • wp-includes/plugin.php: apply_filters()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Codex

    Date in Heading Using $my_date Variable
    Returns the date in the default format inside an <h2> tag and assigns it to the $my_date variable. The variable’s value is then displayed with the PHP echo command.

    
    $my_date = the_date( '', '<h2>', '</h2>', false );
    echo $my_date;
    
  2. Default Usage
    Displays the date using defaults.

    
    <?php the_date(); ?>
    

    Date as Year, Month, Date in Heading
    Displays the date using the ‘2007-07-23’ format (ex: 2004-11-30), inside an <h2> tag.

    
    <?php the_date( 'Y-m-d', '<h2>', '</h2>' ); ?>
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文