Category Template Hierarchy - WordPress Plugin ( Download )

Note: This page is out of date, please review the documentation in the ‘Description’ and ‘Other Notes’ area of this plugin’s WordPress Plugin Directory page.

Adds parent-category.php and child-category.php with all the normal hierarchical behavior of the native category templates. This greatly extends the natural hierarchy of theme templates with regard to categories. Theme developers can now easily create separate templates for categories with have children, the children themselves, and categories which are neither.

Also adds is_parent_category() and is_child_category() functions for easy use in themes.

Note: This plugin does not actually create parent-category.php, child-category.php or any of their related templates - rather it modifies the native [template hierarchy]( to allow theme developers to create specific templates for parent and child categories

Developer Notes: Template Hierarchy

What follows are the lists by which the new hierarchical elements will cascade - realize that all these lists are essentially just branches off of the existing Template Hierarchy.

If category has no children and has no parent (stand-alone category):

  • category-{slug}.php
  • category-{term_id}.php
  • category.php
  • archive.php
  • index.php

If category is a parent (has children):

  • parent-category-{slug}.php
  • parent-category-{term_id}.php
  • parent-category.php
  • category.php
  • archive.php
  • index.php

If category is a child (has a parent):

  • child-category-{slug}.php
  • child-category-{term_id}.php
  • child-category.php
  • category.php
  • archive.php
  • index.php `

Developer Notes: Conditional Tags

With this plugin comes two additional conditional tags which behave much like any other in WordPress. In a similar fashion to how one might use is_category(), developers may, with this plugin, use the following functions:

  • is_parent_category()
  • is_child_category()

Description

These conditional tags checks if the page being displayed is for hierarchical category that has children (e.g. is a parent category), or has a parent (is a child) respectively. They are boolean functions, meaning they return either TRUE or FALSE.

Usage

1
 //AND 

Parameters

$category (integer/string/object) (optional) Category ID, Category Slug, Category Object. Default: Current Category

Note: Unlike is_category(), these functions will not take arrays of categories or category titles. I’ll work on that. Sorry.

Return Values

(boolean) True on success, false on failure.

Examples

1
 is_parent_category() is_child_category()  // When any parent/child category archive page is being displayed  is_parent_category( '9' ); is_child_category( '9' ); // When the archive page for Category 9 is being displayed AND its a parent/child. is_parent_category( 'blue-cheese' ); is_child_category( 'blue-cheese' ); // When the archive page for the Category with Category Slug "blue-cheese" is being displayed AND its a parent/child. 

Notes

  • Clearly there is a need to have is_child_category_of() and is_parent_category_of(). They will be made available in future releases.

Known Bugs

  1. [Fixed] In 1.0 and 1.0.1 there was a “happy accident” which I permitted, wherein if a category was both a parent and a child, it would look for a parent-child-category.php chain of templates. I later realized that in such a case the plugin would only look for that chain, as it would overwrite the previous chain of templates. 1.0.2 eliminated this “happy accident”
  2. [Future fix] Revealed by bug #1, the current logic makes it such that if a category is both a parent and a child, the child relationship will take precedence over the parent relationship. In such a case, only the child-category.php chain of templates will work with for that category.