Monday, November 26, 2018

Displaying the Date and Time in the WordPress Loop

Displaying the Date and Time in the WordPress Loop

Adding the date and time to the WordPress Loop seems like a simple enough task, right?

Well, yes. It can be a case of coding a simple template tag and letting WordPress do the work for you. But sometimes you can run into problems along the way.

In this quick tip, I'll show you the different functions WordPress gives you for displaying the date and time, and tell you which ones to use if you run into snags.

The Available Template Tags

WordPress gives you four functions to output the date and/or time. These are:

  • the_date(). By default it will echo the date of the post in the format F j, Y, so if the post was published on 20 November 2018, it would echo November 20, 2018.
  • get_the_date() - this fetches the date and doesn't echo it out. To echo it, you'd use echo get_the_date() which gives you the same result as the_date(). It's useful if you're already using echo in your code. It can also help you get round the problem of dates not being displayed, as you'll see shortly.
  • the_time() and get_the_time() - these fetch the time by default, but if you specify date formatting, you can also include the date. You could even this just to output the date if you configured the formatting to do so, but it would more sense to use the_date() or echo get_the_date().

Formatting the Date

Each function has a default format, which you can override if you need to. To do this, you'll need to use standard PHP date and time formatting.

Here are some examples, all for a post published on 20 November 2018.

  • the_date() would output November 20, 2018 by default.
  • echo get_the_date( l, S M Y ) would output Tuesday, 20th Nov 2018.
  • the_time( 'g:i a' ) would output 2:03 pm.
  • echo get_the_time( 'G:i' ) would output 14:03.
  • the_time( 'g:i a, D, j F y' ) would output 2:03pm, Tues, 20 November 18.

Troubleshooting Dates in the Loop: Missing Dates in an Archive Page

If your'e using the_date() to output dates on an archive page, and you find that the date isn't being displayed with some posts, that's because the_date() doesn't repeat the date for subsequent posts published on the same day as a first.

Sometimes you might want to keep it like this, if you don't want to repeat the date for every post published that day.

But if you want to ensure that all posts have their date output with the title and any other content you're outputting, you'll need to use another function. You can use any of the other three functions above.

The simplest option is to replace the_date() with echo get_the_date(). If you wanted to add the time, either the_time() or echo get_the_time() will work.

Note: If you're puzzled by the fact that the_date() throws up this problem, but the_time() doesn't, it's because posts published on the same date weren't published at the same time. You'd have to go to a lot of effort, either scheduling posts, editing the publication times, or co-ordinating efforts between two bloggers, for that to happen!

I had this problem in the front page template of a theme I coded for a client. In this template were a number of loops, all leading to different content types on the site and all coded using WP_Query. The problem wasn't apparent until the day they added two posts, (not something they normally did). They were puzzled as to why the second post's date didn't appear on the home page, and kept editing it, refreshing it and republishing it until they gave up and asked me.

Here's the original code:

I edited the one function so it read like this:

In the line beginning li class="home newsletter", I replaced the_date( 'j, F' ) with echo get_the_date( 'j, F' ). It fixed the problem.

So if you ever find that dates aren't showing up in your archives, this might be the solution.


No comments:

Post a Comment