How about combining the three templates into just one category template file, inside it have a switch statement which breaks 3 ways, using the relevant template. This would gather all your handling into just one file.
Just how many different categories are you handling, if it is just 20 or even 30 it would still be possible to just copy the files, use a script so that you can regenerate them from your 3 templates.
I think I’d have to write a conditional, saying If category 33,432,547,329, do this, else do that.
Which would work, but when new categories are created you have to go edit the template file. The thing is, this already works for pages: you make a new page, you assign it a template.
I agree that having to edit template files for new categories would be really cruel.
Instead of categories you could assign tags, say t1, t2, t3. Then in your master posts template you would have if tag t1 then template1
Well its not that editing templates is so much work, but rather that I’m on contract to build a site. When I’m gone they still need to be able to maintain their site, so its better if they can do it in the admin.
Your idea is interesting, although I’d use custom fields rather than tags. I’ll post what I come up with.
Here is my solution:
First, 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. Then, I created three category templates: category-standard.php, category-sorted.php, category-tabbed.php.
Second, make a template redirect to load the correct template by finding the custom field value:
function category_template_redirect() {
if(is_category()) {
$this_cat=get_the_category();
$template_field=get_field('template', $this_cat[0]);
if (have_posts()) {
include(TEMPLATEPATH . '/category-' . $template_field . '.php');
die();
} else {
$wp_query->is_404 = true;
} } }
add_action( 'template_redirect', 'category_template_redirect' );
This function goes in your functions.php file.
Excellent work.
Is it now appropriate to mark this thread as complete?