WordPress, themes, templates and errors


Ok, so I’ve got WordPress installed and running, and now I’m trying to figure out how it all works behind the scenes. I’m a bit of a geek that way, you see. I want to be able to tweak the layout and operation of my site to suit my own needs, and not what anyone else thinks I should want. For example, although it’s set to show ten posts per page by default with the most recent post at the top, I’ve tweaked the monthly archive page so that it shows every article in that month, with the oldest at the top. That way, you can start at the top of the page and just read everything chronologically.

Now, it’s taken me a little while to get to grips with something here, and I wanted to write it down before I lost track of what it was. WordPress uses something it calls “the loop” to display posts on the screen. The basic format of it is:

 do we have any posts to display? 
      Yes: loop through them one by one and show them
      No: show a short “no posts founds” message instead.

It this “short message” I’ve been trying to understand.

Each theme has template pages, and I’m concerned here with three of them:

  • index.php, which is the default page
  • archives.php, which is the template for monthly and category archives
  • 404.php, which is an error page displayed if it can’t find what you’re looking for.

Both the index and the archive templates have the same loop structure as defined above, and both have their own short messages.

The reason I’m looking into all this is that I want to put a “next month” and “previous month” link at the bottom of the monthly archive page, but not every month will have entires, since I abandoned the blog for quite a while.

So, I’m calling up a page to show me the a monthly archive for a month that has no posts. Still with me?

If the theme defines a 404 error page, the short message in either template file never seems to be displayed. It always redirects to the 404 error page. So, why define the short message in the template in the first place? There must be a reason why it’s there, you can’t have code defined in a file that’s been downloaded thousands of times, and modified by hundreds of people when they write their own themes, if there’s no point to it at all. If you take away the 404 error page, the theme defaults back up to index.php and shows the short message defined there, instead of showing the one in archives.php.

For ages I couldn’t work out why it was never showing the short error message in the archives template. And then it dawned on me. It’s to do with how you’ve set your blog to show permalinks. If you have it set to the default permalink (www.mysite.org/?p=123) then the short message in each template is displayed regardless of whether a 404 template is defined or not. If you use a pretty permalink (www.mysite.org/2009/05/sample-page/) then the short message in the archives template never shows up. It always redirects to the 404 page, and if that doesn’t exist, it shows the short message in the index.php file.

I don’t know if this helps me write the “next month/previous month” link code, but at least I’ve worked out the answer to something that’s been bugging me for ages.

Comments are closed.