18

I have custom post type "press." and made a page template the queries posts of that type. The posts are supposed to show their published date, but if there are more than one posts with the same date, only the first post shows the date and the others don't. Is there a way to show the date of every post?

enter image description here

<?php get_header(); ?>
<?php
$wp_query = new WP_Query();
$wp_query -> query('post_type=press&showposts=100');
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div id="press">
<div class="press-item cf">
<div class="press-img"><a href="<?php the_field('link'); ?>"><?php the_post_thumbnail('medium');?></a> </div>
<div class="press-content">
<div class="press-title"><a href="<?php the_field('link'); ?>"><?php echo get_the_title(); ?></a> </div>
<div class="press-excerpt"><?php the_excerpt(); ?> </div>
<div class="press-date"><?php the_date(); ?></div>
</div>
</div>
</div>
<?php endwhile;  ?>
<?php get_footer(); ?>
38

I faced similar issue in past because I modified my date function. And then posts were displaying date if only each post has different date otherwise it returned blank.

Try adding <?php echo get_the_date(); ?> instead.

26

Why does it not show?

When you look at the source of the the_date() function, then you will notice two globals:

global $currentday, $previousday;

And then there's a rule if there's a date to display ... or not. The check is similar to the one done with is_new_day():

if ( $currentday != $previousday ) {

    // show date

    // Set global
    $previousday = $currentday;
}
// else
return null;

As you can see, the $previousday instantly gets set to $currentday;. So it gets echo-ed once. Right after that, both days are the same and the check will fail. That's the reason why your first post displays it, but the others don't show it.

Why does it show?

If you ask yourself why it then shows more than one date, after the global gets euqalized, then you will have to take a look at setup_postdata(). This function gets called by the_post(); and is responsible for setting up everything for a single post in the loop.

if ( have_posts() )
{
    while ( have_posts() )
    {
        the_post(); # <-- Calls setup_postdata( $post );

        // your loop stuff here
    }
}

The internals of setup_postdata() are quite easy to understand (at least to what the globals get set):

$currentday = mysql2date('d.m.y', $post->post_date, false);
$currentmonth = mysql2date('m', $post->post_date, false);

So the moving part is $previousday against which the $currentday global gets set and checked. And unless there is new day, the_date() won't display anything.

Just set your posts to totally different days and suddenly you will see the date appear on each post.

What's the idea behind that?

Actually the idea is pretty simple and present since v0.7.1 - at least this is what the phpDocBlock states: Why would you like to display the date for each post in an archive? An archive looks like this:

+--------------+
| 28.10.2014   |
+--------------+
| Post Title A |
| Post Title B |
+--------------+
| 29.10.2014   |
+--------------+
| Post Title C |
| Post Title D |
+--------------+

You don't agree with that? Well, then you are simply using a function that was intended to be something totally different.

Why does get_the_date() work and how to use it the right way

It is not affected by the if/else in the the_date() function (the globals check). It also does not have the filter. How to fix that? Simple:

echo apply_filters( 'the_date', get_the_date(), get_option( 'date_format' ), '', '' );

This adds any callbacks attached to the the_date filter to your custom output. It also uses the default date_format option setting as default - which is used by the_date() as well. And it avoids any before and after values - again, exactly as the the_date() function.

  • 1
    Thanks kaiser. That explains why sometimes it fails to print date. Thanks a lot. – Robert hue Oct 29 '14 at 1:58
  • 1
    Very comprehensive explanation. Better than accepted answer. – Nilambar Oct 29 '14 at 3:43
  • 1
    @Nilambar The accepted answer is the tl;dr version and perfectly fine :) – kaiser Oct 29 '14 at 3:48
  • props for digging into this @kaiser - more insight than I ever expected on such a mundane issue – staypuftman Oct 13 '16 at 0:02
  • 1
    This answer is better than the WP docs explanation. – Ian Apr 9 '17 at 22:19
10

Don't use the_date(), instead use the_time().

the_date returns the date only, the_time returns the date + the time. I don't know the reason why wordpress won't return more than one date when the_date is used in a loop. But it has to do with the fact that the value is the same. If you use the_time the value is never the same, therefore it always returns the value. So you can print something like <?php the_time('F j, Y'); ?>

This link from the codex, explains how the_date works much better than I do.

1

Brute force it.

<?php the_time('F j, Y'); ?> - <?php the_time(); ?>

Worked for me, displays full date, time for each posts regardless if it is the same date or not.

0

It only shows up once because that's how a diary would work. It would show the date at the top and you would write your entries below it. I use this code the display it the dates for each post using the_time.

<?php the_time(get_option('date_format')); ?>

For more info I wrote a tutorial about it here: Why the_date() Function in WordPress Only Shows Up Once on the First Post

  • Please note if you want to promote your own product/blog you must disclose your affiliation, otherwise your answer may be flagged as spam. Please read How to not be a spammer – DavidPostill Apr 9 '17 at 11:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.