Web Development

Solution: the_date() not working in WordPress

While developing my this new theme I ran into the situation where date for some posts were not displaying. After some review I found this is occurs when I have multiple posts published on same day.

I was surprised with this behavior but after quick searching what I found is that I have made very silly mistake on this.

What I have missed is very first notice on the [code]the_date()[/code] page of WordPress Codex. Let me copy the same line over here.

SPECIAL NOTE: When there are multiple posts on a page published under the SAME DAY, the_date() only displays the date for the first post (that is, the first instance of the_date()). To repeat the date for posts published under the same day, you should use the Template Tag the_time() or get_the_date() (since 3.0) with a date-specific format string.

So basically [code]the_date()[/code]) will show date only once for the same date posts that’s why I was not getting date for some posts.

Solution

[cc lang=”php”]
// This won’t show date in all cases
the_date(‘F d, Y’);

// This will show date in all cases
the_time(‘F d, Y’)
[/cc]

Shares:
  • MM
    MM
    January 29, 2013 at 8:59 am

    I had a same problem before and same as here codex comes to the rescue.

    Reply
  • Venture
    Venture
    January 29, 2013 at 12:42 pm

    Very nice Article with great solution. Thanks for the share.

    Reply
  • MM
    MM
    December 20, 2019 at 2:55 pm

    I had a same problem before and same as here codex comes to the rescue.

    Reply
  • Venture
    Venture
    December 20, 2019 at 2:55 pm

    Very nice Article with great solution. Thanks for the share.

    Reply
  • Salih
    Salih
    April 12, 2013 at 9:43 am

    I had the same issue But got resolved with this Codex answer –
    http://wordpress.org/support/topic/wrong-day-of-month-displaying-while-using-the_timel-f-j-y

    Reply

Leave a Reply

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