Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-05
7
Medium Priority
?
1,008 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 1000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

885 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