Forked from tcmulder/wp-subcats-use-parent-cats-template.php
Created
June 19, 2013 21:30
-
-
Save zenman/5818263 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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