0815

Du musst dich anmelden um Beiträge und Themen zu erstellen. Einloggen · Registrieren

Display Dates for Groups of Posts

In order to display the date that a post was published, you have two options:

  • <?php the_time(); ?> — displays the date for each and every post
  • <?php the_date(); ?> — displays the date only once for each group of posts published on a certain day

So, if you want to list the post date next to each post, use the the_time(). If, on the other hand, you have multiple posts on any given day, use the_date() to list the post date only once for each days’ posts. Something like this:

// the_time();

	Jan 01, 2009 - Post #1
	Jan 01, 2009 - Post #2
	Jan 01, 2009 - Post #3

	Jan 02, 2009 - Post #4
	Jan 02, 2009 - Post #5
	Jan 02, 2009 - Post #6

	Jan 03, 2009 - Post #7
	Jan 03, 2009 - Post #8
	Jan 03, 2009 - Post #9

// the_date();

	Jan 01, 2009
		Post #1
		Post #2
		Post #3

	Jan 02, 2009
		Post #4
		Post #5
		Post #6

	Jan 03, 2009
		Post #7
		Post #8
		Post #9

See the difference? Good, because I’m not going to explain the concept any further. Instead, I will move on by showing you how to use either template tag in your loop. This really doesn’t need explaining either, but for the sake of completeness, here it is. First, the_time() tag:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

	<h1><?php the_title(); ?></h1>
	<p><?php the_time(); ?><p>
	<?php the_content(); ?>

<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

This loop will output the post title, date, and content for every post. Alternately, here is how to use the_date tag:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

	<?php the_date('','<p>','</p><hr />'); ?>
	<h1><?php the_title(); ?></h1>
	<?php the_content(); ?>
 
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

This loop will output the post title and content for every post, and also output the date for each group of posts published on any given day, as explained above. Notice we are adding the paragraph elements for the post date based on whether or not the date is actually output. If it is, then the date will be wrapped in <p> elements; if it’s not, no <p> elements will be output, thereby preventing repetitive sets of empty paragraph elements. Make sense? Good. Here are the available parameters for the nifty the_date() template tag:

<?php the_date('date format', 'before the date', 'after the date'); ?>




0