How to Add date to a post in WordPress

WordPress, primarily being a blogging platform, has blog posts at the centre of it. While designing a WordPress theme, more often than not, there is a need to add a custom layout for posts. And while creating the layout, usually the date of the post needs to be displayed. In this article, we are going to see how to add a date to a post in WordPress.

When The Loop is  executed, Post Object (WP_Post) is created for each post and through this, all the information about the post can be retrieved. Specifically for date, it can be retrieved using $post->post_date. The output of this is in the ‘YYYY-MM-DD 00:00:00’ format. But what we need is just the date in a proper format. Luckily, WordPress takes care of the format. You can select the format for posts in Dashboard > Settings > General > Date Format. You can also define your custom layout but we’ll get to it later. Right now, let’s focus on outputting the date for posts in the Loop.

WordPress has a couple of functions to output the date – the_date() and get_the_date(). Both can be used to output the date for a post but each one is to be used in certain conditions. For this, we need to understand the difference between the two. the_date() function prints the date of post, but it works only one time. It means that if it is used in a loop, it will output the date for only a single post instead of all the posts in a loop. This function runs only once in a loop. This functionality makes it ideal to be used to show single post content.

On the other hand, get_the_date() prints nothing, but returns the date of the current post in the loop. And it does it individually for every post in the loop. This makes it perfect for use in a loop. Keep in mind that it will need to be printed using echo  or printf as you see fit for your project.

These functions accept arguments which you can see for yourself in the official documentation ( the_date() and get_the_date() ). The main argument I want to focus is the format. You can specify the format for the date using PHP standards. Even if no arguments are specified, the function fetches the arguments from Dashboard > Settings > General > Date Format.

So that’s about it for outputting the date of a post in WordPress. If we are in the loop, we need yo use get_the_date() and in single post page, we use the_date(). Hope you learned something from this article.

Meet the Author

Divjot Singh

Divjot is an experienced WordPress developer and technical writer with more than a decade of experience in WordPress Development. Whenever he's not creating amazing WordPress experiences, he can be found on the road with his bike.

Meet the Author

Divjot Singh

Divjot is an experienced WordPress developer and technical writer with more than a decade of experience in WordPress Development. Whenever he's not creating amazing WordPress experiences, he can be found on the road with his bike.

Leave a Reply

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