Since moving over to WordPress, one of the things I’ve been trying to do with this site is get the SEO a bit more firmed up. As CMSs go WordPress is already pretty good on the SEO front, but there’s a few niggles here and there, some of which are a little harder to sort out than others.
Once you’ve divided your site’s pages into categories, WordPress handily provides a function which will enable you to present them on your site as a menu list of links: wp_list_categories(). In its simplest form, this function iterates through your list of categories, and listing them alphabetically, with nested sub-categories, and outputs them neatly to your page with clickable links. Brilliant.
Let’s say you have a gaming site, www.your-gaming-site.com, and your categories are, say, PC, PlayStation, Xbox, and Wii. Now, when you call wp_list_categories, it will automatically create the following list:
- PC
- PlayStation
- Xbox
- Wii
Great! And this is the corresponding list of URLs that it produces for the links:
- http://www.you-gaming-site.com/category/PC
- http://www.you-gaming-site.com/category/PlayStation
- http://www.you-gaming-site.com/category/Xbox
- http://www.you-gaming-site.com/category/Wii
Wonderful! Well… yes, and no. For me, at least, I wasn’t happy having the word /category/ included in the URL. To me, it’s unnecessary clutter, and doesn’t suit how the rest of my permalinks had been constructed – which do NOT include the word /category/ in the links.
Within WordPress, it’s known as the category base prefix, and though you can change it, you can’t currently remove it – well, not as such. If you want to have it disappear, the simplest means is to change the category prefix to “/.” (slash dot). This effectively removes it from visitors’ eyes – but provides the slightly ugly URL structure in HTML as:
- http://www.you-gaming-site.com/./PC
- http://www.you-gaming-site.com/./PlayStation
- http://www.you-gaming-site.com/./Xbox
- http://www.you-gaming-site.com/./Wii
This will work fine – at least to the user, but may produce complications for search engines.
So, I looked to see if I could tweak the wp_list_categories() function, and though it has many variations possible, at time of writing this is one feature that can’t be altered. Reading around I found that other people had come across this problem but nobody had found a simple, clean solution.
So I came up with my own – and this is it.
echo str_replace("/category/", "/", (wp_list_categories(echo=0')));
This line of PHP calls wp_list_categories(), but with a couple of tweaks. First, using the echo=0 parameter, the HTML it generates is returned as a string value, rather than written straight to the page. Then, assuming that the base category prefix of “category” is still used, using the core PHP function str_replace, all instances of “/category/” are removed from the returned string, and replaced with a single ”/”. The newly modified HTML string is then written to the page using echo, and voila! The links appear as before, but with /category removed. In our example above, this produces the following, much tidier list of URLS:
- http://www.you-gaming-site.com/PC
- http://www.you-gaming-site.com/PlayStation
- http://www.you-gaming-site.com/Xbox
- http://www.you-gaming-site.com/Wii
WordPress, it should be said, warn against using the category to begin permalinks in this way, because, it says of performance issues. So, be warned. However, it’s how I wanted things to go, and others may too. Hope it works for you.
At time of writing: using WordPress 3.2.1, PHP5