Set the date properly on your WordPress posts

When building WordPress themes and plugins, we need to display the date next to our post in several places such as the single post and archive view. We have more than one function that can do the trick for us.

Displaying the publish date on single posts

For single posts, there are two options for displaying the publish date.

One way to display the post published date is to use:

the_date();

Another way to show the publish date is to use:

the_time();

Both functions can take PHP date and time parameters.

These basic functions work great for single posts but would they always work properly on an archive page or a list of posts? Nope! If you have more than one instance of the function with the same date, only the first post will display the date.

Displaying the publish date on archive pages

To avoid some dates not appearing on archive pages we will use the_time() function but instead of returning the time we will ask the function to bring back the date format from our WordPress main setting page located in Settings > General.

Setting the date format 

By using the_time instead of the_date we will avoid the “missing date” problem and will also have more control over the date format in our plugin/theme development.

Copy/paste and use in your project

the_time(get_option('date_format'));

Leave a Reply

Your email address will not be published. Required fields are marked *