• Resolved photocurio

    (@photocurio)


    I realize that I can make template files such as category-xxx.php. But I have a site that has lots of categories, and I only need three different templates for them.

    I’d like to add a custom field to the ‘edit category’ page, and use that to select the template that will display the category archive (Advanced Custom Fields can add custom fields to categories).

    That would be similar to how page templates work. It would eliminate a lot of unnecessary files and duplicated code.

Viewing 6 replies - 1 through 6 (of 6 total)
  • RossMitchell

    (@rossmitchell)

    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.

    Thread Starter photocurio

    (@photocurio)

    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.

    RossMitchell

    (@rossmitchell)

    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

    Thread Starter photocurio

    (@photocurio)

    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.

    Thread Starter photocurio

    (@photocurio)

    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.

    RossMitchell

    (@rossmitchell)

    Excellent work.
    Is it now appropriate to mark this thread as complete?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Can I assign a category template just like a page template?’ is closed to new replies.