Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for Canada

asked on

Wordpress Year/Month Archive List – 'for' loop fix

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 a post for a particular custom post type. I haven't quite been able to figure it out. 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 ,&#9;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) :
&#9;$year_current = $month->year;
&#9;if ($year_current != $year_prev){
&#9;&#9;if ($year_prev != null){?>
&#9;&#9;
&#9;&#9;<?php } ?>
&#9;
&#9;<li class="archive-year"><a href="<?php bloginfo('url') ?>/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a></li>
&#9;
&#9;<?php } ?>
&#9;<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 an embedded 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.

Any help would be greatly appreciated. Thanks.
Avatar of sivagnanam chandrakanth
sivagnanam chandrakanth
Flag of India image

Avatar of Adam

ASKER

Thanks sivagnanam_c. You may not have realised but the code that I posted is actually from that WPBeginner tutorial you suggested I look at. And I've tried the Smart Archives Reloaded plugin already and it doesn't quite do what I'm looking to do.

I appreciate the help but I just want to try and come up with a solution based on the code I posted in my question (or something that allows me to do the same thing).
ASKER CERTIFIED SOLUTION
Avatar of jagssidurala
jagssidurala
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam

ASKER

Thanks very much for the help jagssidurala. To be honest, I understand what it is you're suggesting but unfortunately, my PHP skills are not that strong. I have been playing around with it for a bit but not having much luck. I'll keep trying but if you could show me how the code should look (using my sample above), it would be extremely helpful.
Avatar of Adam

ASKER

Not quite what I was looking for but I appreciate the help nonetheless.