Home Articles An archives page with all the posts in cronological order

An archives page with all the posts in cronological order

2270
78
SHARE

An archive page is nothing more than a page with links to your blog posts ordered by date, category, etc. In this tutorial we’re going to list all the blog posts in our blog, ordered by date in a hierarchical way.

Something like this:

  • May 2008

    • 25 – Lorem ipsum
    • 12 – Dolor sit amet
  • April 2008

    • 22 – Adipiscing elit
    • 18 – Consectetur
    • 3 – Ipsum amet

Cool, eh! Let’s go…

The page

First create a new php file and type this at the very beginning:


This will help us identify the template when we create the archives page in the WordPress back-end.

Now, let’s enter the basic page code. You may want to open the page.php file of your theme, and copy the structure. I’ll be using the default “kubrick” theme structure for this tutorial:



The code

Ok and now this is the code that will print the actual list of posts:



	

	post_date);
	$month = mysql2date('n', $post->post_date);
	$day = mysql2date('j', $post->post_date);

	?>

	

		
		
		

		

  • <a href="">

How does this work? Well… it’s a little hard to explain. Basically, we’re using the get_posts() function to get all the posts (numberposts=0 means all of them), ordered by date (orderby=post_date) in descending order (order=DESC). Then, we’re looping through every last one of them, comparing it’s published month and year to the previous one and echoing the year and the month accordingly.

Wrapping up

That’s basically it. Now save this file and upload it to your template directory (/wp-content/themes/your_theme). Then create a new page in the WordPress back-end, selecting “Hierarchical Archives” in the page template drop-down menu. And you’re done.

Hope you liked the pseudo-tutorial ;-).

78 COMMENTS

  1. Excellent tutorial. Very handy.

    I’d love to see the same process written up for creating a Tags page. Seeing all of the tags with post data below each in alphabetical order, instead of the typical tag cloud, would be quite interesting.

  2. I guess, “numberposts” option should be set to -1 in order to get all posts.
    ex: get_posts(‘numberposts=-1&orderby=post_date&order=DESC’);

    if it is set to 0 you will get limited number of posts as it is defined in your settings page. (settings > reading > blog pages show at most)

    at least, it works in that way with wordpress 2.6.2.

    good luck!

  3. I just noticed that the last post printed in the list isn’t followed by a closing tag for that month’s ul. That’s a huge issue. Would appreciate a correction to fix that.

  4. PERFECT! this is exactly what i needed! (for a friend’s website, not mine.) i see you incorporated people’s suggestions. thanks so much for the script!!!

  5. Super – I needed something quick and simple for my wife’s blog and this did the trick. I previously installed about 4 plug ins which didn’t work and this code worked great. Thanks for posting and being ranked high on google!

  6. Wow, I can’t believe how perfect this is for what I needed!

    Can you think of any way to get the Month title to display the number of posts next to it?

    Eg ‘September 2008 (10)’

    Thanks!

    • Undeniably believe that which you said. Your favorite justification appeared to be on the net the simplest thing to be aware of.

      I say to you, I certainly get annoyed while people
      consider worries that they plainly don’t know about.
      You managed to hit the nail upon the top and also
      defined out the whole thing without having side effect ,
      people can take a signal. Will likely be back to get more.
      Thanks

  7. Hi. This post is exactly what I’m looking for, but I’m getting an error when I try to use it:

    Parse error: syntax error, unexpected T_VARIABLE when it hit the lines

    $previous_year = $year = 0;
    $previous_month = $month = 0;
    $ul_open = false;

    Any idea what’s wrong?

    Thanks for the help!

  8. Just what I needed! And so much easier without having to do ‘real’ PHP 😉

    Is there a possibility to also seperate by year, e.g. by just adding something like an hr tag or a div=”divider” between every year?

  9. this is awesome. I have a little thing to ask. How can I made like this;

    -2011
    –March
    —post01
    —post02
    —post03
    –February
    —post01
    —post02
    —post03
    –January
    —post01
    —post02
    —post03
    -2010
    –December
    —post01

    Thanks

  10. You at smashing, are reading in minds ! I just started to build my first wp theme saturday and you release this post. I was blocked, I think this will really help me ! Thanks !

  11. Works like a charm in WP 3.1
    Only one question: How can u make it, that it will list the posts from last three months or only year 2011 ?

  12. I’ve been using this for over a year now but after upgrading to wp 3.1.3, this is no longer working for me. The archives page works only when I’m logged in as admin but when I log out, the page won’t load. Any thoughts?? I’d really hate to look for another archiving tool because I really love how easy it is to use this one!

  13. hi, does anyone know how to add pagenavi plugin to have pagenavigation on the page? like with normal archive/posts?

  14. Hi!
    Nice post.
    I want to have posts separated by date. Any idea how can I do that?
    Below is the example of what I want to achieve

    12 Aug 2011

    post
    post

    13 Aug 2011
    post
    post
    post

    • Does your website have a contact page? I’m having a tough time locating it but, I’d like to send you an email.
      I’ve got some recommendations for your blog you might be interested in hearing. Either way, great site and I look forward to seeing it grow over time.

  15. I’m trying to figure out how to display post thumbs instead of a list of post titles… any suggestions on how I might accomplish that or where I can read up?

    Thanks!

  16. I’m trying to display them in a grid… sort of a portfolio view. I may have to just play with styling a bit more… but got em in there.

    Thanks for posting this!

  17. Hi there

    Just wanted to say thanks a million for this. Been searching for ages and tried numerous plug-ins which didn’t do what I wanted them to, and couldn’t quite get my head around the php. You have saved the day. Consider yourself bookmarked.

    Dom

  18. I used the Smart Archives Reloaded plugin to do this for standard posts on my development site, but could not figure out how to achieve the same for custom post types.

    This works perfectly.

    Thank you.

  19. I cannot believe this just works like a charm. This should be a part of the WordPress documentation in the archives section!!
    Honestly, I am so glad I found your site as it is exactly what I was looking for. I have been using a plugin to achieve the same before (but I’m getting some annoying error messages with it) and am more than thrilled to see that such little code is required to achieve the same. Without flaws.

    Thank you so much, WordPress Guy! You made my day. =)

  20. Thanks a lot buddy… I was looking for such format of archives page… I was using another function, it was working fine but only for /%postname%/ permalink, the_permalink(); wasn’t working with that function…

    I am happy with your work, of course I made some changes to meet my needs…

    Thanks again

  21. It worked like a charm. I would like to have the same page but also with all categories at hop hierarchy :

    Category A
    * May 2008
    25 – Lorem ipsum
    12 – Dolor sit amet
    * April 2008
    22 – Adipiscing elit
    18 – Consectetur
    3 – Ipsum amet

    Category B
    ….

    Can you help me for this ?
    thanks !

  22. Hi, Im trying to do this but based on a custom field date not publish date.

    I have stored the custom field date as yyyy-mm-dd.

    Any ideas how I can alter your method to achieve this?

    Thanks

  23. Hi, have good tutorials about archive page but what about if we need to page number show at bottom next prev page… limiting the posts… hope you understand my question..

    thanks 🙂

  24. I’m so glad I clicked past the first page of Google search results to find this! So simple and effective. Why does everyone else have to make things so complicated!?! (Oh, but I did rewirte your PHP code a lot, not sure why you kept closing and reopening your PHP tags so much 😉 )

Comments are closed.