the_date()
Displays or Retrieve the date of the current post was written. Works with a group of posts (published on the same day).
Uses only in a WordPress loop.
Works with a group of posts means that for posts published on the same day, the date will be displayed only for the first post, even if the function is called several times for each post. I.e., for each next post in the loop, this function checks on what day current post was published, and if the date of the current post and the previous one are the same, then the date of current post will not be displayed.
HTML output can be filtered with the_date hook. Date string output can be filtered with 'get_the_date'.
Affects on the return value of is_new_day() when both functions are used in the same code construction.
Hooks from the function
Return
String|null
.
- String if retrieving.
- null when the result is displayed on the screen.
Usage
<?php the_date( $d, $before, $after, $echo ); ?>
- $d(string)
- PHP date format.
date_format
option if not specified. See possible formats.
Default: '' - $before(string)
- Text/HTML before the date.
Default: '' - $after(string)
- Text/HTML after the date.
Default: '' - $echo(true/false)
- Whether to echo the date or return it. true - echo result.
Default: true
Examples
#1 Output the date in the format of WordPress settings
<p>Published at <?php the_date(); ?></p>
#2 Output the date in the 2007-07-23
format and wrap it in the <h2> tag
<?php the_date( 'Y-m-d', '<h2>', '</h2>' ); ?>
Displays something like this: <h2>2018-05-12</h2>
#3 Return the date to variable, but not display it
$date = the_date( 'Y-m-d', '', '', 0 ); //> 2018-05-12
Notes
- Global. String. $currentday The day of the current post in the loop.
- Global. String. $previousday The day of the previous post in the loop.
Changelog
Since 0.71 | Introduced. |