Skip to content

Instantly share code, notes, and snippets.

@zenman
Forked from tcmulder/wp-subcats-use-parent-cats-template.php
Created June 19, 2013 21:30
Show Gist options
  • Save zenman/5818263 to your computer and use it in GitHub Desktop.
Save zenman/5818263 to your computer and use it in GitHub Desktop.
<?php
/*
* Use Parent Category
* Description: Causes subcategories to use their parent's template
*/
function load_cat_parent_template(){
global $wp_query;
if (!$wp_query->is_category) {
return true; // saves a bit of nesting
}
// get current category object
$cat = $wp_query->get_queried_object();
// trace back the parent hierarchy and locate a template
while ($cat && !is_wp_error($cat)) {
$template = get_template_directory() . "/category-{$cat->slug}.php";
if (file_exists($template)) {
load_template($template);
exit;
}
$cat = $cat->parent ? get_category($cat->parent) : false;
}
}
add_action('template_redirect', 'load_cat_parent_template');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment