Solved

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

Posted on 2013-02-05
7
990 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
ID: 38854472
0
 
LVL 1

Author Comment

by:Adam
ID: 38854480
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
ID: 38872894
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
ID: 38873186
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
ID: 38875749
Not quite what I was looking for but I appreciate the help nonetheless.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Responsive Design or Mobile Sites? 4 30
Dynamic varibles 5 32
php extract($_REQUEST) 5 46
How has this flight information been added to this website? 7 18
Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 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

18 Experts available now in Live!

Get 1:1 Help Now