Breaking News
Loading...
Jumat, 01 Februari 2013

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:
<?php
/*
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:
<h1>Archives</h1>
<ul>
<?php
$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; ?>
</ul>

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:
<ul>
<?php wp_get_archives('type=monthly'); ?>
</ul>

You can see this live already.
Wanna make money with your website?

Original Post: Building an “Archives” Page with Simple PHP

0 komentar :

Posting Komentar

Back To Top