Archive for the Wordpress Category

On another Wordpress blog that I have (on which I post a log more regularly than I do on this one…), I have been having trouble every time I have upgraded Wordpress lately. After the upgrade, I get an error that looks like this:

Warning: array_key_exists() [function.array-key-exists]: The first argument should be either a string or an integer in /xxx/webroot/wp-includes/category-template.php on line 176

It turns out that this is caused by the theme that I am using not setting categories correctly due to using functions that have been deprecated in recent versions of Wordpress. Basically, no category is set on the home page, resulting an error when the Wordpress code tries to look up the category name on the home page.

You can fix this by getting a new version of your theme if there is one available, or if there is no new version then change to a different theme. However, if you are particularly attached to your current theme and there is no new version of it (which is the situation in which I find myself) then this error is actually quite easy to fix by editing category-template.php:

Go to line 176, which looks like this:
if(array_key_exists($category, $categories))

and replace it with this:
if(!(false === $category) && array_key_exists($category, $categories))

This checks for the existence of the category variable before attempting to use it. Of course, you will need to do this every time you upgrade as when you upgrade, the new category-template.php will overwrite the one that you have edited. And the line number might change too, but the error message will tell you that.

And don’t forget to make a backup of your original category-template.php just in case the editing goes awry.

Good luck!