display catgory child page the same as parent for all subcategories
Description
This function will allow all child categories to follow the same layout of its parent category. This code is good because you will not have to create a new page for each category. In order to use you must add the php code to the functions.php and create category.php file for each catogory number for […]
This function will allow all child categories to follow the same layout of its parent category. This code is good because you will not have to create a new page for each category. In order to use you must add the php code to the functions.php and create category.php file for each catogory number for the parent. category.php files are titled (category-108.php) where 108 is the catory number. Catgory.php is similar to page.php. Inside the category.php use general WP functions
PHP Source Code
<?php
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' );
?>