Why would the_date() be empty in my wordpress loop?

In my custom theme, I came across a weird issue displaying the_date() which took me a good few hours to solve.

I am using Foundation 6 and thought I’d somehow messed up the block grid elements. I rewrote the code and the issue was still there.

Where’s the date gone?

There can be a number of reasons that the date is blank, but for mine, it was because I had two posts which were on the same day. I had the following code:

<?php the_date('d.m.Y'); ?>

This worked nicely on two of the four posts, but not the other two which remained blank.

How to fix the blank date field in your loop

 

There is a special note about the_date() on the wordpress website which reads:

SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()).

That’s it! They were the same date. To fix, simple change the_date() to get_the_date(). My new code looks like this:

<?php echo get_the_date('d.m.Y'); ?>

Now it shows both the missing dates. A simple fix, but one which took me a while to solve.

Read more information on the_date().

 

Leave a Reply

2 Comments on "Why would the_date() be empty in my wordpress loop?"

Notify of
avatar
Sort by:   newest | oldest
Nathan Noom
Nathan Noom

Thank you for posting this. You just saved me a lot of headache and research.

wpDiscuz