Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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';

  endforeach;
 return $single;
}
share|improve this question

1 Answer 1

up vote 0 down vote accepted

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/

share|improve this answer
    
category-{slug}.php applies a template to category archive pages. –  Pollux Khafra May 6 '12 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. –  Pollux Khafra May 6 '12 at 1:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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