categories - Theme for subcategories

admin2025-06-06  2

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?

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?

Share Improve this question edited May 23, 2017 at 11:33 CommunityBot 1 asked Feb 18, 2015 at 21:40 AlvaroAlvaro 1611 silver badge13 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 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.

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');
    }
} 

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
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749142726a316729.html

最新回复(0)