0

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

1 Answer 1

3

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; ?>
3
  • 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, 2011 at 16:57
  • sorry, it was adapted from another file that does something slightly different. I edited the code above.
    – cbaigorri
    Nov 23, 2011 at 18:20
  • Thank you. I just had to add </ul> outside the loop to close the last list. Thx :)
    – flintsburg
    Nov 23, 2011 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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