A tricky little problem. In my blog roll, some articles were missing their dates. My code:
<time>
<?php the_date();?>
</time>
Well, <?php the_permalink(); ?>, <?php the_title(); ?> worked just fine.
Why wouldn’t <?php the_date(); ?> output the date for random posts?
The solution
<time>
<?= get_the_date();?>
</time>
The explanation
the_date() function only outputs the date if it is different than the date of the previous post. Eyeroll.
I am not sure how this could be useful, but who knows. Ref: WordPress codex the_date()
get_the_date() will always return the date, so use that instead. Ref: WordPress codex get_the_date()
Note: Make sure you add an echo statement to your code: <?php echo get_the_date(); ?> or the short tag version <?= get_the_date(); ?>. That’s needed because the_date() retrieves AND displays the date, but get_the_date() only retrieves it without printing it.
Confusing!
We are so used to conventions, that an unexpected behavior like this can become a huge timewaster to troubleshoot. I still cringe every time I run into one of those issues.
Leave a Reply