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 ;-).
On WordPress 2.6, it only shows the last month.
Or maybe it displays only number of posts that you set in Options -> Reading.
I just have the last 5 posts… as I set to display on front page.
(See http://www.designgrafik.net/archives/ )
Forget it. I’m using another PHP script now…
What other PHP script did you use to create an Archive?
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.
Thank you so much for this tutorial, it’s been a lifesaver for me!
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!
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.
@Travis: You’re totally right. What a dumb mistake!
Thanks for the heads-up
@Baturalp: You’re right… I guess I should double-check my code before posting it 😉
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!!!
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!
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
Great help, thanks. Is there any way to add in page navigation? If your archives go a really long time, typical page navigation would come in very handy. Thanks.
This is AWESOME!! Thanks so much!
Hi,
I have been working on an archive page template myself and your post came in handy to understand the get_posts methods in WordPress. Thanks for these explanations.
If you wish to do so, you can check out my archive page on my blog at http://www.wwangle.com/blog and grab the code here: http://www.wwangle.com/blog/2009/a-wordpress-index-page-template/
Samantha
Thanks a ton WP Guy – this was exactly what I was looking for too. Cheers!
Nice tutorial bro… thank
its will be helpfull for me.. 😀
i will wait your next tutorial…
This is absolutely perfect! I’ve tried a few others and none of them work.
Quick question — I want to remove the Date headers and want to display just a series of posts with the date. When I take the date header out, however, there is a line break in between the groups of posts in a month. How do I get rid of that?
Example of what’s happening: http://www.grabup.com/uploads/9bd9152b2d986045e564fd894ec41f3e.png
That’s probably because you have a bunch of elements… which probably have margin or padding.
Try with this: http://pastie.org/388072
Thanks for the tutorial. I was able to create an “Archive” page for my lifestream videos following this http://askadria.com/archive/
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!
Thanks for the great tutorial! This has been in the back of my mind for awhile, and it’s exactly what I needed!
Hi
Great I was looking for that.
Thanks!
Great tutorial. Thanks much!
awesome , this was driving me crazy! works like a charm on wp 2.8.4
Btw did you know you misspelled cronological? it is really spelled as chronological…
Nice site btw!
This really helped a lot! Copy / pasted and worked as intended. Thanks!
[…] wp_get_archives on the template because I needed more data out of it. With a lot of help from the wordpress guy, I did something very similar but separated the year and the month. I also show an icon of the […]
Brilliant.
You explained that so well I managed to create my first php template. And it worked, first time. Thanks!!!
Really a nice tutorial. Thank you so much for the great post.
Awesome, this is exactly what I needed. Thank you!
[…] partie du code php de la page provient d’un article sur le blog WPGuy : WP Guy. Posté dans WordPress | Tags : archives, dynamiques, jquery, […]
This is perfect! Thank you so much. It works perfectly.
fantastic, i can easily input the code, thanks for this tutorial and the code
This works very well. Thank you!
Thanks! Works perfectly. Here is the example archive page on my site. http://devonenote.com/archive/
Note that you might need to update the css class in code to the actual class in your theme.
[…] An Archives Page With all the Posts in Chronological Order by WPGuy 10 Ways to Customize Thesis and Enhance Your Blog by Matt Flies More Showing, More Hiding by Learning jQuery […]
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?
If I want all posts in a certain category to appere. How to do then becouse I wont 7 post´s on the main page but 50 post´s in two category´s.
Cool tip! I wanted to show all posts on my website and numberposts=0 made it work for me! Thanks
this is fantastic! thanks so much! works great!
[…] rujukan : Zo’C Blog , WordPress Guy , WordPress Codex Entri Lain :Nickname : Asal Usul Eastbullet[Contest] IT Contest By […]
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
oh I trying but i can’t get it. Could I have any help please.
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 !
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 ?
Thanks for a wonderful post.
I did mine here:
http://nail-fungus-cures-that-work.com/blog/?page_id=239
Thanks once again
Hammer
I was looking to achieve something like this without the use of a plugin. Thanks for the great solution!
[…] the invoices including whether they have been paid or not. To do this I found a great post by the WordPress Guy named An archives page with all the posts in chronological order. I modified this slightly to change the class that was assigned to the <li> to show whether […]
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!
hi, does anyone know how to add pagenavi plugin to have pagenavigation on the page? like with normal archive/posts?
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
No clue what any of that code means but it did exactly what I have been wanting for my archives page. Thanks!
Hi there, thanks a lot….i was trying many things but,,,this only worked finally.
u r great!!!
Hey, thanks a lot. Worked very well.
How can I display certain number of post per page?
Exactly what I needed! Thanks for saving this php novice about a day’s worth of coding!
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.
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!
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!
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
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.
Thanks – great code, saved me a lot of time.
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. =)
If you desire to increase your experience just keep visiting
this site and be updated with the most up-to-date gossip posted here.
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
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 !
Thank you so much! Exactly what I needed. You’re super cool!
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
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 🙂
Thanks,
Worked well
Thanks for this post it does “just what it says on the label”!!
Worked well! Thanks
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 😉 )
very nice article in every sense
I was looking for this solution. tried many other methods but this worked well for me.
Comments are closed.