Solved

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

Posted on 2013-02-05
7
983 Views
Last Modified: 2013-02-11
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.
0
Comment
Question by:Adam
  • 3
7 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
Comment Utility
0
 
LVL 1

Author Comment

by:Adam
Comment Utility
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).
0
 
LVL 12

Accepted Solution

by:
jagssidurala earned 500 total points
Comment Utility
You are retrieving months with year, also retrieve distinct years list individually. write a one more foreach above the existing one.

and generate year li with first foreach, generate month li with second foreach if year in first foreach matches with year in second foreach.
0
 
LVL 1

Author Comment

by:Adam
Comment Utility
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.
0
 
LVL 1

Author Closing Comment

by:Adam
Comment Utility
Not quite what I was looking for but I appreciate the help nonetheless.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
The purpose of this video is to demonstrate how to Import and export files in WordPress. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Click on Too…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now