Created
May 29, 2014 06:34
-
-
Save mayeenulislam/c3bbd3cc71189f9eb0f5 to your computer and use it in GitHub Desktop.
WordPress: Force sub-categories to use the parent category template - werdswords.com
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
function new_subcategory_hierarchy() { | |
$category = get_queried_object(); | |
$parent_id = $category->category_parent; | |
$templates = array(); | |
if ( $parent_id == 0 ) { | |
// Use default values from get_category_template() | |
$templates[] = "category-{$category->slug}.php"; | |
$templates[] = "category-{$category->term_id}.php"; | |
$templates[] = 'category.php'; | |
} else { | |
// Create replacement $templates array | |
$parent = get_category( $parent_id ); | |
// Current first | |
$templates[] = "category-{$category->slug}.php"; | |
$templates[] = "category-{$category->term_id}.php"; | |
// Parent second | |
$templates[] = "category-{$parent->slug}.php"; | |
$templates[] = "category-{$parent->term_id}.php"; | |
$templates[] = 'category.php'; | |
} | |
return locate_template( $templates ); | |
} | |
add_filter( 'category_template', 'new_subcategory_hierarchy' ); | |
// SOURCE: http://werdswords.com/force-sub-categories-use-the-parent-category-template/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment