0

I am working with categories (cities) and subcategories with the same menu (for example: cars, animals, houses).

Therefore right now my categories structure looks like this:

- London
-- cars 
-- animals
-- houses

- Manchester
--cars
--animals
--houses

The slugs for each subcategory (as it seems they have to be unique), are named like this category_name + subcategory_name looking like london-cars, manchester-cars.

Now, I would like to use a different template for my main categories (cities) than for the subcategories (cars, animals, houses).

I've read this topic that suggest a way to do it, but there's a big problem with it: I would need to create as many conditions as subcategories I have within all categories.

This makes it unmaintainable.

The other option I found was to create use the theme category-slug.php, but this would also imply that I have to create as many subcategory themes as subcategories in all sections.

Is there any other way to do it?

1

You could use the template_redirect hook to check and see if your post is a category and then whether it is a sub-category ... and if so, force a different template.

For example (assuming you are using wordpress categories)

function my_maybe_override_category_template( $template ) {
    # Make sure you are about to show a category term
    if ( is_category() ) {
        # Grab the term that content is to be displayed for
        global $wp_query;
        $term = $wp_query->get_queried_object();
        if ( 0 < (int)$term->parent ) {
            # This term has a parent, try to find your special child term template
            $found = locate_template('template_child_categories.php');
            if ( !empty( $found ) ) {
                # Override the normal template as we found the one we created
                $template = $found;
            }
        }
    }
}
add_action('template_redirect', 'my_maybe_override_category_template');

Unless I got something slightly off, that should do what you are wanting provided that:

  • You create a special template called template_child_categories.php, which will be used to show your child terms.
0

Ended up doing this, which seems much more simple:

if (is_category()) {
    $this_category = get_category($cat);
    if (get_category_children($this_category->cat_ID) != "") {
        // This is the Template for Category level 1
        include(TEMPLATEPATH.'/location.php');
    }
    else{
        // This is the Template for Category level 2
        include(TEMPLATEPATH.'/subcategory.php');
    }
} 
0

Problem with above structure is slugs of sub-categories.

USA > (slug: usa)
-News (slug: news)
-Attractions (slug: attractions)

Once above is done and you create same structure with more Categories, you will get problem is slugs, see example below

Canada > (slug: canada)
-News > (slug: news-canada)
-Attractions (slug: attractions-canada)

One possible solution is if you reverse the case

Make SubCategory as Parent Category and tag them by country name, see example below.

News > Posts have tag: USA, Canada, etc
Attractions  > Posts have tag: USA, Canada, etc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.