Advertisement
rexcoder

Force sub-categories to use the parent category template

Mar 31st, 2019
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.84 KB | None | 0 0
  1. function pmq_subcategory_hierarchy() {
  2.     $category = get_queried_object();
  3.  
  4.     $parent_id = $category->category_parent;
  5.  
  6.     $templates = array();
  7.  
  8.     if ( $parent_id == 0 ) {
  9.         // Use default values from get_category_template()
  10.         $templates[] = "category-{$category->slug}.php";
  11.         $templates[] = "category-{$category->term_id}.php";
  12.         $templates[] = 'category.php';
  13.     } else {
  14.         // Create replacement $templates array
  15.         $parent = get_category( $parent_id );
  16.  
  17.         // Current first
  18.         $templates[] = "category-{$category->slug}.php";
  19.         $templates[] = "category-{$category->term_id}.php";
  20.  
  21.         // Parent second
  22.         $templates[] = "category-{$parent->slug}.php";
  23.         $templates[] = "category-{$parent->term_id}.php";
  24.         $templates[] = 'category.php';
  25.     }
  26.     return locate_template( $templates );
  27. }
  28.  
  29. add_filter( 'category_template', 'pmq_subcategory_hierarchy' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement