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

asked on

Custom Post Type – Year/Month Archive List

Hi there,

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

Any help would be greatly appreciated. Thanks!
Avatar of COBOLdinosaur
COBOLdinosaur
Flag of Canada image

What exactly are you expecting from that code.  All I see are a serious of links to the top of the page.  If you are trying to go to a particular position in the page then you put an id on the target and then use the id in the link like this:
href="theid#"

This article will show you how to style targets

Cd&
Avatar of Adam

ASKER

Sorry COBOLdinosaur, but I think you're misunderstanding what I'm asking. I guess what I didn't make clear in my question is that I'd like to do this for my Wordpress site.

In the sidebar on my site, I want to display links for the years/month of all the posts for a particular post-type. So when a user clicks let's say "2013", it will take them to an archive page that shows them all posts from 2013. If they select "November 2012", they will go to an archive page for all November 2012 post, etc....

The sample code I added to my question was just to demonstrate the structure of what I wanted. I guess I could have just written something like this:

2013
-- January
-- Feburary

2012
-- November
-- December

etc..

So I'm looking for some code that will create a list similar to this. Does that make sense?
I don't really do much Wordpress support, and I don't understand why a simple list would be difficult.  So the best I can do is post some links dealing with Wordpress archives.  

http://wordpress.org/extend/plugins/custom-post-archives/
http://mark.mcwilliams.me/wordpress/custom-post-type-archives/
http://en.support.wordpress.com/archives-shortcode/
http://wpguy.com/articles/an-archives-page-with-all-the-posts-in-cronological-order/
http://yoast.com/wordpress-archive-pages/

Perhaps an Expert who is actively supporting Wordpress will come along with more detailed help.

Cd&
Avatar of Adam

ASKER

Thanks for the links Cd&. I am familiar with Wordpress and have searched around for solutions but can't seem to find one that does exactly what I'm looking for.

Hopefully someone else will come along and help out. Thanks anyway.
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America 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 jason1178! I think this will work for my purposes. One small thing though—the code generates something along the lines of 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

However, I am hoping to have the structure of the unordered list like the following:

<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="#">December</a></li>
    </ul>
  </li>
</ul>

Open in new window

I thought that I just needed to make a slight modification to the code but I can't seem to get it to work. Any suggestions?
It's not the easiest thing in the world to do given how the foreach loop is constructing the entries.  This is why they specifically give you a CSS solution to offset the years from the months.  I would urge you to keep that intact and use CSS to indent month <li> tags or otherwise make them stand out from year <li> tags.
Avatar of Adam

ASKER

Thanks jason178, however, the years/months need to go in a drop-down menu (you hover over the year then a sub-menu appears with the archive months) and it would have been much easier to work with if the lists were structured the way I presented them in my question. Also, the structure I'm looking for would be better semantically and for accessibility purposes.

Sorry if I'm being a bit picky. I do appreciate the help.
That's alright.  From my end, though, I don't have the time or inclination to pull apart the foreach loop and test see if the element is first/last and then add the extra <ul> tags as needed.  You may want to close this now (with or without points, your call) and repost the foreach code in the PHP TA and see if someone there is willing to rewrite it for you.
Avatar of Adam

ASKER

Thanks again for the help jason1178. I will create a new question as you suggested and hopefully someone will be able to help modify the code to get it to where I need.