Building an “Archives” Page with Simple PHP
In my opinion most websites should have an HTML sitemap (also called “Archive” or “Archives”). That is basically a page that will contain a link to all other pages of the site. In case of a blog it will contain a link to all blog posts.
Such HTML sitemap is beneficial both for search engines and human visitors. Search engines benefit from it because it becomes easy to crawl all the content of the website. Human visitors, similarly, become able to quickly look for a post that was published on a particular month/day/year. On top of that there’s an SEO benefit, as your link juice will spread evenly across all pages.
In order to build such an “Archives” page I used to use and recommend a plugin called SGR Clean Archives. That plugin is quite old, however, and it stopped being updated a while ago.
Then a couple of days ago I received an email from a reader who took my recommendation and was using the plugin. He said that his server was becoming really slow lately, and upon further investigation they discovered that the plugin was hammering the server and causing the slowness.
He also pointed out that my own “Archives” page was not working anymore. Damn! My guess is that the latest versions of WordPress rendered the plugin’s code slow and ineffective.
Anyway I needed to find an alternative. Since performance was a crucial aspect here I decided to implement my own “Archives” page using raw PHP and WordPress functions. It turned out to be an easy task:
1. Create A Template
The first step was to create the page template I would use for the “Archives Page”. Basically I copied the structure of a normal page (usually page.php), and added the following piece of code on top:
Template Name: Archives
2. Add the PHP code
After that I removed the piece of code that outputs the normal content:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(__('Read more'));?>
<?php endwhile; else: ?>
<?php endif; ?>
And substituted it with my own PHP code to output the links of all published posts:
$args = array( 'numberposts' => 1000 );
$lastposts = get_posts( $args );
foreach($lastposts as $post) : setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
3. Create the Page
After that all you have to do is to create a page inside WordPress and make it use the “Archives” template. That’s it.
I am using that code on my archives page right now. The only thing I am not happy with is the fact that all links are mixed together. I would like to separate them month by month, to make it easier for humans to find what they are looking for.
As soon as I solve that problem I’ll post an update, so stay tuned.
Update: Somehow the code above was not working with 2000+ posts. With 1000 or fewer it works fine though. Since I have more than 2000 posts published I opted for a simpler archive with links to the monthly pages, with the following code:
<?php wp_get_archives('type=monthly'); ?>
You can see this live already.
17 Responses to “Building an “Archives” Page with Simple PHP”
@Abdul Rehman, the default archives on WP is usually on the sidebar, hence this solution.
@Avik, I changed it because the plugin I was using was not working anymore, and even the code I came up with wasn’t fast enough to process all my 2000+ posts.
Whoa, I eat code almost daily, that was such a simple but powerful trick Daniel 🙂
For anyone who want to get to know more about any other option related to this, you can check WordPress Codex about this here http://codex.wordpress.org/Function_Reference/wp_get_archives
Happy blogging 🙂
Your earlier archives page was much better. Dont know why you changed. I am looking for a plugin that will show the post titles too. It will be helpful for those who have much less than 1000 posts. What’s your take on?
Thank you for your practical solution.I think everybody needs to try this.
Yes its fine but in blogger and wordpress, its by default so why we need this ???
Thanks for this I never knew how to create my own sitemap before.. I have always used wordpress plugins to generate them for me… I also have image sitemaps and video sitemaps as well.. I haven’t tested to see what sort of difference it makes having them but I have not been relegated because of them so they must be doing something! 🙂 haha
Keep up the good work 🙂
I think this is the most practical solution which you have shared today.
This information is great simple step by step that I was able to follow. A lot of technical stuff like this sometimes scares me and even confuses me. Though this information was excellent.
Thanks for sharing,
Great post Daniel. I could see your archives page. It looks great.
Awesome post Daniel.
I noticed your archive page was not working from quite some time.
I guess the problem persists!
Though I can figure out both the links i.e. one on the menu bar and another on the article but nothing appears on the landing page.
BTW I am using, clean archives reloaded plugin by Viper.
I think this post is getting to technical for me 😉 If you folks found any Plugins that can do the job automatically, do share it out since I’ve read a copy of times the importance of HTML sitemap in relation to the SEO.
By the way, not sure whether is it me alone as I noticed that the ad banner on the left panel blocked part of the text from the post, I am using IE9 (however, when I use Safari in my iPhone, no such issue). You may want to check it out.
I think there is some problem with your archive page. It is totally blank for me.
I use “Smart Archives Reloaded” plugin to create archives page in my blog. It is a great plugin for me. 🙂
@Hakan and gestroud, there’s a link to it right on the article, and on the main navigation bar. You couldn’t find it, or the page is not loading for you?
@Hakan I was going to ask the same question.
So Where is your Archives Page 🙂
Comments are closed.