Assigning Templates to CategoriesApril 4, 2015
WordPress has an odd way of handling category templates. You are supposed to create a file called category-wildlife.php for the ‘wildlife’ category. While that is intuitive and simple, suppose you have 50 categories on your site, and 3 category layouts?
You want to be able to edit a category, and assign it a template from your list of three choices. That’s how page templates work. You certainly don’t want 50 template files for your 50 categories.
Here is the way to do it:
Use Advanced Custom Fields to put a custom field named ‘template’ on your categories. I made radio button fields, with one button corresponding to each category template that I’m using: standard, sorted and tabbed. I set standard as a default field.
Create three category templates: category-standard.php, category-sorted.php, category-tabbed.php.
Make a template redirect to load the correct template depending on the value of the template field. Paste it into your functions file.