$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>categories - Theme for subcategories|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

categories - Theme for subcategories

matteradmin9PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far