Group Posts by Date on Your Homepage
If you’ve ever wanted a way for your visitors to quickly scan down a list of your posts on your homepage, then the following little trick will allow you to group your posts together by date so they will look something along the lines of the following:
It’s always recommended to make a child theme first so that you won’t need to touch your original theme.
Step 1: Find the spot in your index.php where the “content” begins. This will often be identified with a div that looks something along the lines of the following (yours may differ a little):
[php]<div id="content" role="main">[/php]
Step 2: After finding the opening div, go toward the bottom of the page and find the closing div for the content. This div will look something along the lines of the following (again, your theme may differ – there may be no notations for the closing div):
[php]</div><!– #content –>[/php]
Step 3: Delete everything between the opening content div and the closing content div.
Note: This is a radical step that will completely wipe out your posts on your front page (temporarily). For this reason you should make sure you have a copy of your original index.php file in case something goes wrong.
Step 4: Place the following code in between the opening content div and the closing content div.
[php]
<?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_permalink() ?>"><?php the_time(); ?> <b><?php the_title(); ?></b></a></li>
<?php
$day_check = $day;
endwhile; ?>
[/php]
Step 4: Add some styles to make it look pretty and nice.
(Code adapted slightly from wpmu.)