Link to home
Create AccountLog in
Avatar of Adam
AdamFlag for Canada

asked on

For loop help – Year/Month Archive list

Hi there,

I have a Wordpress site and I'd like to create a list of archive links to the years/months that I have posts (without using a plugin). I haven't quite been able to figure it out. With regards to the structure, here is essentially what I'm looking for:
<ul>
  <li><a href="#">2013</a>
    <ul>
      <li><a href="#">January</a></li>
      <li><a href="#">February</a></li>
    </ul>
  </li>
  <li><a href="#">2012</a>
    <ul>
      <li><a href="#">November</a></li>
    </ul>
  </li>
</ul>

Open in new window

I have found a website with a tutorial that provides code that gets me very close to what I need.
<?php

global $wpdb;
$limit = 0;
$year_prev = null;
$months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month, YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = 'publish' and post_date <= now( ) and post_type = 'post' GROUP BY month , year ORDER BY post_date DESC");
foreach($months as $month) :
	$year_current = $month->year;
	if ($year_current != $year_prev){
		if ($year_prev != null){?>
		
		<?php } ?>
	
	<li class="archive-year"><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a></li>
	
	<?php } ?>
	<li><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a></li>
<?php $year_prev = $year_current;

if(++$limit >= 18) { break; }

endforeach; ?>

Open in new window

The problem is, the list that this code generates looks like this:
<ul>
  <li><a href="#">2013</a></li>
  <li><a href="#">January</a></li>
  <li><a href="#">February</a></li>
  <li><a href="#">2012</a></li>
  <li><a href="#">December</a></li>
</ul>

Open in new window

As you can see, the list structure is not quite like what I'm looking for (the months should be a nested list within the respective year). I thought it would be pretty straightforward to just modify the code and get it to create the list as I need (and it probably is) but I haven't been able to figure it out.

I have to admit, I've posted this question a few times without much luck. To clarify, I'm not really looking for a plugin or an alternate solution to this (unless I can get the same results). Any help would be greatly appreciated. Thanks!
SOLUTION
Avatar of Pyromanci
Pyromanci
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
I didn't include the opening and closing <ul> tags as I did not see them in your original code so I left them out. As for if there is a better way, this the most simplistic way I know of to do what your doing. I actually use this method often for a internal company reporting site, cause it's so simple it's easy to expand when you need to other things.
Avatar of Adam

ASKER

Thanks again Pyromanci.