Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-02-05
7
Medium Priority
?
1,004 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

704 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