Title: Display child categories using parent category template (was category-x.php)
Last modified: August 19, 2016

---

# Display child categories using parent category template (was category-x.php)

 *  Resolved [mleacy](https://wordpress.org/support/users/mleacy/)
 * (@mleacy)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/category-xphp/)
 * I would like to use the same .php file to display a category and all child categories.
   Is there a way to do this?
 * My category with category ID 6 displays properly via category-6.php. It has a
   child category, and when that child category is selected, the child is displayed
   using index.php.
 * I have a number of categories that I would like to organize like this. I would
   like:
 * Category X and all of Category X’s children to display using category-x.php
 * Category Y and all of it’s children should display using category-y.php
 * Thanks in advance for any and all tips.

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [17 years, 2 months ago](https://wordpress.org/support/topic/category-xphp/#post-961146)
 * Interest challenge, try this as a plugin:
 *     ```
       <?php
       /*
       Plugin Name: Child Force Category Template
       Plugin URI: http://wordpress.org/support/topic/234874
       Description:
       Author: MichaelH modified original from Mark Jaquith
       Version: 0.0
       Author URI:
       */
   
       /* GPL goes here */
   
       function child_force_category_template() {
   
           if ( !is_category() || is_404() ) return; // we only care about category views
   
           $cat = get_query_var('cat');
           $category = get_category ($cat);
   
           if ( !($category) ) return; // no category with which to work
   
               if ( file_exists(TEMPLATEPATH . '/category-' . $category->cat_ID . '.php') ) {
                   include(TEMPLATEPATH . '/category-' . $category ->cat_ID . '.php');
                   exit; }
               elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->category_parent . '.php') ) {
                   include(TEMPLATEPATH . '/category-' . $category->category_parent . '.php');
                   exit; }
       }
       add_action('template_redirect', 'child_force_category_template');
       ?>
       ```
   
 * Code was taken from [http://txfx.net/code/wordpress/force-category-template/](http://txfx.net/code/wordpress/force-category-template/)
   and the idea of category parents inspired by cSoul comment (#52) on that post.
 * Thank goodness you didn’t go into category z, a grandchild of x, because this
   only works for parent/child relationships, not parent/child/child of child.
 *  [Begin](https://wordpress.org/support/users/bentalgad/)
 * (@bentalgad)
 * [16 years, 9 months ago](https://wordpress.org/support/topic/category-xphp/#post-961434)
 * Hi !
 * I have the same problem,
    but i didn’t and understand, where to put this code,
   and what excecly it doe’s?
 * thank’s
 *  [amummey](https://wordpress.org/support/users/amummey/)
 * (@amummey)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/category-xphp/#post-961466)
 * I modified the function a bit, I think it works a little better, you need to 
   put the function inside your functions.php file in the current template directory.
   This actually uses the callback for only category templates, so don’t need to
   worry about it affecting anything else.
    Here is the code:
 *     ```
       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->category_parent . '.php') ) {
               $cat_template = TEMPLATEPATH . '/category-' . $category->category_parent . '.php';
           }
           else{
               $cat_template = $template;
           }
   
         return $cat_template;
       }
       add_action('category_template', 'child_force_category_template');
       ```
   
 *  [sshilo](https://wordpress.org/support/users/sshilo/)
 * (@sshilo)
 * [16 years, 3 months ago](https://wordpress.org/support/topic/category-xphp/#post-961483)
 * where do you limit which categories this will work with? for example, if I only
   want it to work on category 134 and it’s children.
 * thanks.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Display child categories using parent category template (was category-
x.php)’ is closed to new replies.

 * 4 replies
 * 5 participants
 * Last reply from: [sshilo](https://wordpress.org/support/users/sshilo/)
 * Last activity: [16 years, 3 months ago](https://wordpress.org/support/topic/category-xphp/#post-961483)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
