WordPress: Customizing Nicer Archives mod

Posted by Pierre Igot in: Blogging
January 25th, 2005 • 3:24 am

The default templates included in WordPress are OK, but lack flexibility. While waiting to find a solution for searching items that match more than one category criterion (I still don’t have a workable solution), I thought I would at least develop a nicer interface to access the WordPress archives on my employer’s web site.

The default interface only lets you view archived items by month, and only full-text items. There is no easy way to provide the user with a simple list of item titles (with links), without the full text of each item.

After searching for a bit and trying a few plug-ins that didn’t work as expected, I found the “Nicer Archives” mod provided by Mark Ghosh.

This is a mod that lets you view archived items as lists of item titles, and also lets you sort archived items by date, title (alphabetically) or category, with an interface that uses user-friendly pop-up menus to select the sort options.

The mod is easy enough to install. (Just copy the .php file to your WordPress directory.) After customizing its appearance to make it match the rest of my web site pages, and translating the little bits of user interface into French, I hit the first major snag.

For some reason, the Nicer Archives mod provides a complete list of all items when the user elects to sort them by category — but when the user elects to sort them by date or title, the mod only provides a limited number of items, if WordPress is configured to limit the number of items per page to a fixed number. My WordPress install is configured to limit the number of items to 12 per page by default — and that means that the Nicer Archives list of items sorted by date or title was limited to 12 items as well.

It doesn’t make any sense, of course. This is an Archives page, which lists archived items as compact lists of item titles. There is no reason to limit the list to 12 items — especially since there is no option to view the rest of the items after that!

My guess is that the Nicer Archives mod simply was not tested properly by its author. I tried to contact him about this, but didn’t get a reply. So I decided to try and figure it out myself.

I reviewed the PHP contents of the Nicer Archives mod, and managed to determine that it does indeed contain two sections, one that applies if the user elects to sort by date or title, and the other that applies if the user elects to sort by category. And it just so happens that the the former section contains the following line:

require_once ('wp-blog-header.php');

whereas the latter does not. The wp-blog-header.php file is a file that most other WordPress pages use and that, as far as I can tell, is the actual engine of the blogging system, because that’s where the mySQL query that will fetch the blog items in the database is built.

So obviously, in the Nicer Archives mod, the Sort by Category section builds its own mySQL query, whereas the Sort by Date/Title section uses the existing mySQL query built by WordPress, with some alterations. And that’s why the item number limit applies to it. The item number limit is actually built into the wp-blog-header.php file, where there is code that retrieves the items-per-page option selected by the administrator in the WordPress options.

I figured that I would just change the line above and make it refer to another file:

require_once ('wp-blog-header-nolimit.php');

I would create this other wp-blog-header-nolimit.php file myself, and makes changes inside it so that the mySQL query doesn’t include the item number limit. Unfortunately, as soon as I changed the line of code above to make it refer to that other file, I got a major PHP error. I got the error even if the wp-blog-header-nolimit.php file was strictly identical to the wp-blog-header.php file, except for its file name.

I have no idea why — and that forced me to give up on that idea. I then decided to alter the wp-blog-header.php file itself, but in a way that would not interfere with the way that other pages use it. After fiddling with various things that didn’t work, I ended up figuring out that I could pass a “nolimit” variable to the file and make it test its value.

In other words, here’s what I added to the wp-blog-header.php file, just before the section where the PHP script builds the actual mySQL query from the various bits that it has been building separately:

if ($_GET['nolimit'] == 1) {
	$limits = '';

The $limits is the string that gets added to the mySQL query and contains “LIMIT 12” when the option is to limit the number of items to 12 per page. By setting it to nothing just before the query is built, I was able to remove the “LIMIT 12” part from the query.

Now the last bit was to pass the nolimit variable to the file. I hoped that I’d be able to include the variable in my customized Nicer Archives file itself, but I couldn’t get it to work. Admittedly, I don’t really know how to declare variables and pass them from file to file. I thought that

global $nolimit;
$nolimit = '';

would work, but it didn’t. So I ended up including the variable in the URI for the Nicer Archives page itself, as in:


And it worked! Phew!

It’s not an ideal solution, far from it. First of all, it means that I have to include the variable in the URI at all times, in all the HREF links to the page. Then it means that I have to have a modified wp-blog-header.php file — which is not supposed to be modified by the administrator as far as I can tell. The more I modify such files, the harder it’ll be to remember all the mods when it comes to upgrading WordPress to a newer version and to reapply these mods after installing the new version. But we’ll cross that bridge when we get to it, I guess.

There is probably a much better way to do this. But unfortunately I don’t know enough about PHP and mySQL to figure it out myself. I am already amazed that I was actually able to get this to work — knowing so little about PHP.

In an ideal world, I’d have a couple of weeks to myself where I could just learn PHP and mySQL scripting properly and become a proper expert. But this is not an ideal world. This is the real world, where I already have more than enough on my plate.

I guess this will have to do.

One Response to “WordPress: Customizing Nicer Archives mod”

  1. … @ a distance … » Sortable Nicer Archives Mod/Hack says:

    […] Take note though, the code will have to be from Mark’s site, and not the older ones, which called up wp-blog-header.php files instead of the wp-config.php file. Those old code generated issues mentioned by latext here. ie, only a limited number of results appeared, based on settings in the WordPress options. And even if someone proposed a fix in the end, it only worked for Date and Title searches, and not for Category. That issue was well brought up by Pierre Igot here. […]

Leave a Reply

Comments are closed.