Reputation: 7532
I recently inherited a website at work. I don't really know PHP.
The below code is supposed to iterate through each post that exists on our wordpress site and post the date that it was posted.
<?php while (have_posts()) : the_post(); ?>
<li>
<a href="<?php the_permalink() ?>" rel="bookmark"><h3><?php the_title(); ?></h3></a>
<span class="the_date">Posted: <?php the_date() ?></span>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
It isn't quite working. It does this, but for posts that land on the same day it doesn't post the date:
How do I get the date to show up for every iteration?
Upvotes: 0
Views: 77
Reputation: 13
<?php while (have_posts()) : the_post(); ?>
<li>
<h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
<span class="the_date">Posted: <?php echo !empty(get_the_date()) ? get_the_date() : date('d/m/Y'); ?></span>
<?php the_excerpt(); ?>
</li>
<?php endwhile; ?>
This adds a fallback to todays date if get_the_date() is not available. Also I have formatted your HTML, as h3 tags should not be allowed within anchor tags.
Upvotes: 0
Reputation: 30210
From the docs:
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.
Upvotes: 4