Advertisement
miriamdepaula

WordPress: Using same template to categories and child categ

Jun 22nd, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. function load_tax_parent_template() {
  2.     global $wp_query;
  3.  
  4.     if (!$wp_query->is_tax)
  5.         return true; // saves a bit of nesting
  6.  
  7.     // get current category object
  8.     $tax = $wp_query->get_queried_object();
  9.  
  10.     // trace back the parent hierarchy and locate a template
  11.     while ($tax && !is_wp_error($tax)) {
  12.         $template = STYLESHEETPATH . "/taxonomy-{$tax->slug}.php";
  13.  
  14.         if (file_exists($template)) {
  15.             load_template($template);
  16.             exit;
  17.         }
  18.  
  19.         $tax = $tax->parent ? get_term($tax->parent, $tax->taxonomy) : false;
  20.     }
  21. }
  22. add_action('template_redirect', 'load_tax_parent_template');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement