Hey guys,
I'm trying to pull this off (see html below) without the use of a plugin.
<ul>
<li>2012
<ul>
<li>December
<ul>
<li>Post title</li>
<li>Post title</li>
<li>Post title</li>
</ul>
</li>
<li>November
<ul>
<li>Post title</li>
<li>Post title</li>
<li>Post title</li>
</ul>
</li>
</ul>
</li>
</ul>
I've come up with this snippet of code which currently outputs this:
December 2012
Post title
Post title
Post title
November 2012
Post title
Post title
How can I change the code below so that it replicates my html code? Please someone help with this, I don't think it's that impossible?
<?php
// Declare some helper vars
$previous_year = $year = 0;
$previous_month = $month = 0;
$ul_open = false;
// Get the posts
$myposts = get_posts('numberposts=-1&orderby=post_date&order=DESC&cat=1');
?>
<?php foreach($myposts as $post) : ?>
<?php
// Setup the post variables
setup_postdata($post);
$year = mysql2date('Y', $post->post_date);
$month = mysql2date('n', $post->post_date);
?>
<?php if($year != $previous_year || $month != $previous_month) : ?>
<?php if($ul_open == true) : ?>
</ul>
<?php endif; ?>
<h3><?php the_time('F Y'); ?></h3>
<ul class="month_archive">
<?php $ul_open = true; ?>
<?php endif; ?>
<?php $previous_year = $year; $previous_month = $month; ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>