Wordpress - Post doesn't show date if there's another post with the same date
I faced similar issue in past because I modified my date function. And then posts were displaying date if only each post has different date otherwise it returned blank.
Try adding <?php echo get_the_date(); ?>
instead.
Why does it not show?
When you look at the source of the the_date()
function, then you will notice two globals:
global $currentday, $previousday;
And then there's a rule if there's a date to display ... or not. The check is similar to the one done with is_new_day()
:
if ( $currentday != $previousday ) {
// show date
// Set global
$previousday = $currentday;
}
// else
return null;
As you can see, the $previousday
instantly gets set to $currentday;
. So it gets echo-ed once. Right after that, both days are the same and the check will fail. That's the reason why your first post displays it, but the others don't show it.
Why does it show?
If you ask yourself why it then shows more than one date, after the global gets euqalized, then you will have to take a look at setup_postdata()
. This function gets called by the_post();
and is responsible for setting up everything for a single post in the loop.
if ( have_posts() )
{
while ( have_posts() )
{
the_post(); # <-- Calls setup_postdata( $post );
// your loop stuff here
}
}
The internals of setup_postdata()
are quite easy to understand (at least to what the globals get set):
$currentday = mysql2date('d.m.y', $post->post_date, false);
$currentmonth = mysql2date('m', $post->post_date, false);
So the moving part is $previousday
against which the $currentday
global gets set and checked. And unless there is new day, the_date()
won't display anything.
Just set your posts to totally different days and suddenly you will see the date appear on each post.
What's the idea behind that?
Actually the idea is pretty simple and present since v0.7.1 - at least this is what the phpDocBlock states: Why would you like to display the date for each post in an archive? An archive looks like this:
+--------------+
| 28.10.2014 |
+--------------+
| Post Title A |
| Post Title B |
+--------------+
| 29.10.2014 |
+--------------+
| Post Title C |
| Post Title D |
+--------------+
You don't agree with that? Well, then you are simply using a function that was intended to be something totally different.
Why does get_the_date()
work and how to use it the right way
It is not affected by the if/else in the the_date()
function (the globals check). It also does not have the filter. How to fix that? Simple:
echo apply_filters( 'the_date', get_the_date(), get_option( 'date_format' ), '', '' );
This adds any callbacks attached to the the_date
filter to your custom output. It also uses the default date_format
option setting as default - which is used by the_date()
as well. And it avoids any before
and after
values - again, exactly as the the_date()
function.
Don't use the_date()
, instead use the_time()
.
the_date returns the date only, the_time returns the date + the time. I don't know the reason why wordpress won't return more than one date when the_date
is used in a loop. But it has to do with the fact that the value is the same. If you use the_time
the value is never the same, therefore it always returns the value. So you can print something like <?php the_time('F j, Y'); ?>
This link from the codex, explains how the_date works much better than I do.