Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to set a group of posts grouped by date as a list. Below is what I would like to have:

Day...
<ul>
    <li><a href="#">Lorem ipsum</a></li>
</ul>
Previous day...
<ul>
    <li><a href="#">Lorem ipsum</a></li>
    <li><a href="#">Lorem ipsum</a></li>
</ul>

I did that:

<?php while (have_posts()) : the_post(); ?>
<?php the_date(); ?>
<li><a href="#"><?php the_time(); ?><?php the_title(); ?></a></li>
<?php endwhile; ?>

I don't really know where to put the < ul > or < /ul > tags. Any idea?

Actually, setting the starting tag is OK by doing this:

<?php the_date('','','<ul>'); ?>

But I can't figure out how to set the closing tag.

Regards, Alex

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I usually perform a simple check to see if that day has changed.

<?php 
$day_check = '';
while (have_posts()) : the_post(); 
  $day = get_the_date('j');
  if ($day != $day_check) {
    if ($day_check != '') {
      echo '</ul>'; // close the list here
    }
    echo get_the_date() . '<ul>';
  }
?>
<li><a href="#"><?php the_time(); ?><?php the_title(); ?></a></li>
<?php 
$day_check = $day;
endwhile; ?>
share|improve this answer
    
I think there is a mistake :/ It gives me something like this: <ul><li><a href="#">...</a></li> </ul><li><a href="#">...</a></li> The closing UL is wrongly placed :( (and the date is no more displayed). –  flintsburg Nov 23 '11 at 16:57
    
sorry, it was adapted from another file that does something slightly different. I edited the code above. –  cbaigorri Nov 23 '11 at 18:20
    
Thank you. I just had to add </ul> outside the loop to close the last list. Thx :) –  flintsburg Nov 23 '11 at 19:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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