Use the same Category Template for all Child Categories
January 4, 2010This is a relatively undeveloped code so it hasn’t been tested and I would not suggest using this for production.
How to use the same category-slug.php template for the category and all child categories?
<?php
/**
FORCE CHILD CATEGORIES TO USE PARENT CATEGORY TEMPLATE
Modified code from http://codex.wordpress.org/User:Lorelle/Custom_Category_Template
v0.2
*/
$jasbt_get_cat = get_category($cat);
// LET'S FIND OUT IF THIS IS A PARENT CATEGORY PAGE
if ($jasbt_get_cat->category_parent == 0) {
$jasbt_get_cat->category_parent = $cat;
} else {
// IF PARENT CATEGORY IS "FOO" THEN USE "/CATEGORY-FOO.PHP" THEMPLATE
$parent_category = get_category($jasbt_get_cat->category_parent);
// Edit parent category name from 'FOO' to either 'ID', 'category-slug', or 'Category Name'
if ($parent_category->cat_name == 'FOO') {
// Edit the category template from 'category-foo.php' to '/category-ID.php' or '/category-slug.php'
include(TEMPLATEPATH. '/category-FOO.php');
exit; // All done.
}
}
// IF NOT FORWARDED, CONTINUE WITH THEME ?>
<?php get_header(); ?>
Explanation: If the cat is a parent category, then it does nothing and continues reading either the category.php file or the archive.php (if it cannot find the category.php file then it will look for the archive.php file; then if that one isn’t there, it will use the index.php file). If it has both, then it will first look at the category.php and in which case you’ll want the code there. If the category.php file is not present, then use this code in the archive.php file.
Development: This code is under development and, to me, looks sloppy but it’s a start.