The great thing about WordPress and PHP is that you can call variables anything you like. The bad thing about WordPress and PHP is that you can call variables anything you like…
Think about these variables:
$post, $category, $template
What do they have in common? Well, they all look like good, descriptive variable names don’t they? What if you changed this to
global $post, $category, $template;
and then put this in a template for a post…?
echo $post; echo $category; echo $template;
Well, you’d get all sorts of interesting things, as
$template are all global variables in WordPress.
FYI: there is a (partial) list of WordPress global variables in the Codex.
So if you use these variable names, and you also access global variables with those names… sometimes you’ll be thinking: why doesn’t my code work? And you’ll find out that when you thought you were changing a local variable you were actually changing a global variable, and that affected something else…
See also: Global variables and understanding scope in WordPress.
I’ve really started to get into using taxonomies in WordPress. One of the early problems I had though was trying to get them to work – so here’s a few things I’ve learned about WordPress taxonomies.
Don’t define taxonomies in functions.php
This is the classic mistake users (and some developers) make. They register (i.e. define) taxonomies in a theme file, change the theme… and wonder where their taxonomies went! (Hint: they’re still there in your database – but if they’re not registered they can’t be accessed.) The solution to this is simple: use a functionality plugin. Then it doesn’t matter which theme you use: as long as the plugin is active, any theme can access your taxonomy data.
WordPress doesn’t have the most robust set of tools for managing taxonomies – you can’t add them via the interface for example, and there are no built in tools for moving things between tags/categories/taxonomies. There are a couple of plugins that I’ve found very helpful:
Want to move terms between tags or categories and taxonomies? Want to rename/combine tags? Term Management Tools does that.
You know those helpful dropdown menus you can use to filter posts by category etc.? TC Custom Taxonomy Filter lets you do that for every taxonomy you define. Very useful.
Making taxonomies appear – aka ‘why doesn’t it work’?
So you’ve defined your taxonomy, you’ve added new items/tags, you click on ‘View’ and you get… a 404 page. Go to Settings: Permalinks and select ‘Save Changes’. (NB: you don’t have to make any changes…!) That will update the rewrite rules and your taxonomy should work… assuming of course that you’ve got posts in that taxonomy.
A taxonomy is just a… category
Don’t believe me? Go to a taxonomy template. Add this:
Categories, tags, taxonomies… all the same thing.