I have single post templates in a folder in my child theme marked "single". I need the sub categories to use the same template as their parent. I've read a dozen tutorials all of which were more than 3 years old and don't seem to work anymore. So what's the correct way to do this? Here's what I was trying.

function pa_category_top_parent_id ($catid) {
 while ($catid) {
  $cat = get_category($catid); // get the object for the catid
  $catid = $cat->category_parent; // assign parent ID (if exists) to $catid
  // the while loop will continue whilst there is a $catid
  // when there is no longer a parent $catid will be NULL so we can assign our $catParent
  $catParent = $cat->cat_ID;
return $catParent;
define(SINGLE_PATH, STYLESHEETPATH . '/single');
* Filter the single_template with our custom function
add_filter('single_template', 'my_single_template');
* Single template function which will choose our template
function my_single_template($single) {
 global $wp_query, $post;
 foreach((array)get_the_category() as $cat) :
    $categoriapadre= pa_category_top_parent_id ($cat);
    if(file_exists(SINGLE_PATH . '/single-cat-' . $cat->slug . '.php'))
    return SINGLE_PATH . '/single-cat-' . $cat->slug . '.php';
    elseif(file_exists(SINGLE_PATH . '/single-cat-' . $categoriapadre . '.php'))
    return SINGLE_PATH . '/single-cat-' . $categoriapadre . '.php';

 return $single;

1 Answer 1


You should try and use the WordPress hierarchy instead of singe-cat-.

Make your parent category template using category.php or category-{slug}.php , and then use the technique here, http://codex.wordpress.org/User:MichaelH/MyPlugins.

Or for more options try this plugin, http://wordpress.org/extend/plugins/category-template-hierarchy/

  • category-{slug}.php applies a template to category archive pages. Commented May 6, 2012 at 0:44
  • I need a template for single post of a parent category to apply to all the single posts in its sub categories. Commented May 6, 2012 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.