Dated Archives in Textpattern, Part 1

One thing that I have seen many times in the TXP Forums, is the desire to use URL’s in the form http://www.yoursite.com/section/yyyy/mm/dd/post-title/and similar. This isn’t possible using a default installation of Textpattern. I hadn’t seen anyone tackle the problem either.

When I first conceived the idea for Pixel Meadow, I envisioned the site using one of two different URL schemes, depending on the section. If it was a “static” section (like About and Projects), then it would use /section/post-title/ URL’s. The blog, however, would use the dated format.

Initially, I was going to write my own CMS that would be up to the job. Then I had a reality check (I don’t have that much time!) and went with TXP. Dated URL’s went on the back burner and I went with /section/post-title/ URLs throughout. Life went on and much of what was originally planned for the site was either shelved or forgotten. It was only when I was tweaking the site recently, that many of my original ideas came back to me. On top of that, I was inspired by an idea that I read about on another site. It was such a cool idea, that I wanted to try it out – but I would need dated URLs for it to work properly.

Finally, I had the kick up the arse I needed to get the job done.

Hackimus Maximus

Actually, it wasn’t too hard to get dated, clean URLs working. It involves hacking your Textpattern installation, so if you want to do it, back up your files first! I won’t be held responsible if it all goes wrong!

You will also need mod_rewrite to get this working properly.

Note. I use TXP Gamma 1.19 – this should still work in v1.0RC1, but I haven’t tested.

1. /textpattern/publish.php

Open up publish.php in your textpattern directory. Scroll down to roughly line 128, which should have a block of code that looks something like this:

Change it to this:

Now look for the following code, in the doArticles function, around line 228:

Change that whole section to this:

Save publish.php. You’ve now got the “engine” of this hack working… If you were to go to your site and enter /index.php?y=2004&m=11&s=[section], you should get all the posts from November displayed.

End of Part 1.

This post is already a little bit longer than I’d hoped, so I’ll cut it there and post the remaining pieces of the puzzle elsewhere.

“But Chris”, I hear you cry. “How do we make our new, fancy, URLs clean?”. Well, for that, young Padawan, you’ll have to wait for Part 2…

Before I go, I will add that if you find any problems or improvements with my code, please share them in the comments!

Advertisements