1. Home
  2. WordPress
  3. How to Fix the_date() Returning Empty Date inside Loop in WordPress
  1. Home
  2. Programming Tools
  3. How to Fix the_date() Returning Empty Date inside Loop in WordPress

How to Fix the_date() Returning Empty Date inside Loop in WordPress

Are you using the_date function inside the loop to display post published date? Well, you might notice the_date() returning empty date for the posts that are published on the same day. For example, if you have two posts published on the same day, then the_date function will return date for the first post and returns empty for the next one. It means, whenever the function the_date is called multiple times, then it will return only one date for all the posts published on the same day. Why does it behave so? I don’t know! Seriously, I couldn’t find much explanation on this. But, WordPress provides multiple functions to replace the_date; that’s you can either use the_time (combined with get_option()) or get_the_date() function.

So, here’s how you can fix the issue.

How to fix the_date() returning empty date when called multiple times?

There are couple of options.

the_date() returning empty date

Option 1: Use the_time()

Starting from WordPress version 3.0, you can combine the_time() function with get_option() to display the post published date.

<?php the_time(get_option('date_format')); ?>

Option 2: Use get_the_date()

You can use get_the_date() function as below:

<?php echo get_the_date(); ?>

Remember to use echo statement before the function, because get_the_date() is not a self displaying function. But the_date() does.

In case, if you want to format the date.

<?php echo get_the_date('j F Y'); ?>

Learn about get_the_date() here.

What if I don’t want to use alternate functions, and I love using the_date()?

Well, then turn Off the behavior.

Add below line in your template and continue using the_date().

global $previousday; $previousday = -1;
Updated on September 2, 2017

Was this article helpful?

Related Articles

Add A Comment