How to use one WordPress category template for multiple categories : So, the original technique that I had posted long time ago is outdated which was a bit hacky and sometime it kinda gets annoying when you have lots of categories and category templates. So, there’s a better approach that can add to your functions.php file to make everything work magically, which was inspired by Justin Tadlock‘s comment on a post by Elliot Jay Stocks.

WordPress has an amazing theming system. It’s not perfect, but if you know the right things you can bend it to your will and do a lot of cool things. One great thing about it is the template hierarchy; WordPress basically allows you to create custom templates for categories, tags, authors, etc.

Just add this code to your theme’s functions.php file and edit as necessary.

For example, to redirect to categories with IDs 1 & 5 to a template called gallery.php, change the if statement to:

This code will look for and use a parent category’s template file whilst showing the appropriate posts.

I had to update it to support slugs in the file name (not just the category ID).

// Use a parent category slug if it exists
function child_force_category_template($template) {
    $cat = get_query_var('cat');
    $category = get_category($cat);

    if ( file_exists(TEMPLATEPATH . '/category-' . $category->cat_ID . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category ->cat_ID . '.php';
    } elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->slug . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category ->slug . '.php';
    } elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->category_parent . '.php') ) {
        $cat_template = TEMPLATEPATH . '/category-' . $category->category_parent . '.php';
    } else {
        // Get Parent Slug
        $cat_parent = get_category($category->category_parent);

        if ( file_exists(TEMPLATEPATH . '/category-' . $cat_parent->slug . '.php') ) {
            $cat_template = TEMPLATEPATH . '/category-' . $cat_parent->slug . '.php';
        } else {
            $cat_template = $template;
        }

    }

    return $cat_template;
}
add_action('category_template', 'child_force_category_template');

 

How to use one WordPress category template for multiple categories

 

However, if the snippet above doesnt works then here is a second category filter hook which you can try which is made up of the original wordpress loops posted on WordPress Codex.

/**
 * Retrieve path of category-videos.php template for 'videos' subcategories
 * Filtered via 'category_template' hook
 *
 * @param string Full path to category template file.
 *
 * @return string Full path to category template file.
 */
function get_category_videos_template( $template ) {
  $category_ID = intval( get_query_var('cat') );
  $category = get_category( $category_ID );
  $parent_ID = $category->parent;
  $parent_category = get_category( $parent_ID );
  /**
   * Check wheter the $parent_category object is not a WordPress Error and its slug is 'videos'
   */
  if ( !is_wp_error( $parent_category ) AND $parent_category->slug == 'videos' ) {
    /**
     * Check if the template file exists
     *
     */
    if ( file_exists( TEMPLATEPATH . '/category-' . $parent_category->slug . '.php' )) {
      return TEMPLATEPATH . '/category-' . $parent_category->slug . '.php';
    }
  } else {
    return $template;
  }
}
add_filter( 'category_template', 'get_category_videos_template' );

 

via WordPress Category Filter Hook & Kristin Falkner

With a system like this, you could technically customize your site to the point where the every category, author, tag, etc. page has a unique look and feel. But, that’s quite a lot of work and in most cases unnecessary. There may be cases though, where you want a small subset of categories to have a unique look, and you want these pages to share this look. Unfortunately WordPress doesn’t allow different pages to share templates.

Assigning a single template to multiple custom post types?

 

Lastly but not the least, OR if you were looking to Assign single template to multiple custom post types?then You can filter template_include and pass your custom template as return value depending on your own conditions.

add_filter( 'template_include', function( $template ) 
{
    // your custom post types
    $my_types = array( 'photography', 'painting' );
    $post_type = get_post_type();

    if ( ! in_array( $post_type, $my_types ) )
        return $template;

    return get_stylesheet_directory() . '/single-photography.php'; 
});