Support » Themes and Templates » Different Template for Subcategory – any conditions?

  • Resolved triplebull

    (@triplebull)


    Is there any option to use different templates for a category and a subcategory? E.g. “category a” should include category-template-level-1.php and “category a > subcategory” should include category-template-level2.php.

    Thanks …

Viewing 4 replies - 1 through 4 (of 4 total)
  • In the case of categories, the hierarchy is fairly simple. For instance, suppose the slug of the Category in question is default and the Category ID is 6. The Template Hierarchy specifies that WordPress will use the first Template file it finds in your current Theme’s directory from the following list:

    1. category-slug.php (Note: available with Version 2.9)
    2. category-ID.php
    3. category.php
    4. archive.php
    5. index.php

    That is, if you do not have a category-slug.php (lets say category-news.php), WordPress will check for a category-ID.php (like category-6.php), and so on.

    So, if you want to make the Category whose ID number is 6 look different from what it is currently (and different from other Category pages), you would want to create a category-6.php file. If you want to make all Category pages look different from other archive pages (such as date and author archives), then you would want to create or modify the category.php file. If you want to make changes to the look of all archive pages, you can create or modify the archive.php file. And if you modify the index.php file, you will affect your entire blog.

    Thank you! But am i right that in this case I have to define every single Category by a category-ID.php page?
    I just want that parent categories use another template than child categories e.g:

    Category a (template-a.php)
    -Subcategory 1 (template-b.php)
    -Subcategory 2 (template-b.php)
    Category b (template-a.php)

    you must have to define either every child category by a cat-id.php page or every parent category by cat-id.php. and rest of all undefined category will use category.php or archive.php or index.php (whichever it finds in your current Theme’s directory according to priority)

    Thanks again. Now I found this solution. Perhaps there is a smarter way but this is much easier than to define every parent id.

    <?php 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(CHILD_DIR.'/maincategory.php');
        }
    		else	{
    			// This is the Template for Category level 2
    			include(CHILD_DIR.'/subcategory.php');
    		}
    
      } ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Different Template for Subcategory – any conditions?’ is closed to new replies.