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.

Here is my issue: I have one parent category with many sub-categories below it, e.g.

  Sub category
        sub-sub category......
        sub-sub category......
        sub-sub category......
        sub-sub category......
        sub-sub category......

I used the WP dTree plugin to show the categories as a tree. Now I want that when I click any sub-sub category...... that it shows all the posts in that category in one custom post template.

I know how to make post template and I know when I make any post if the template file is defined the post will show that template. However I have a lot of "sub-sub category......" categories which contain many posts so it's hard to always define template for each post.

If there's any way to display all the posts in a sub-sub category...... category in one custom template, then please share with me...

Below is an example of what I'm trying to do:

Example of what I'm trying to do

share|improve this question
I'm not exactly following you... 1) Are you displaying a single blog post or a category index archive page? 2) Are you wanting to display the parent category template for the hierarchical categories, or something else? –  Chip Bennett Jun 16 '12 at 20:30
@Chip Bennett .. i updated my post and add one images. if you see that then you can clearly understand what i want to do ... –  pagol Jun 17 '12 at 3:37
add comment

2 Answers 2

Here is what i do, first i add this function to my theme's functions.php file:

 * Tests if any of a post's assigned categories are descendants of target categories
 * @param int|array $cats The target categories. Integer ID or array of integer IDs
 * @param int|object $_post The post. Omit to test the current post in the Loop or main query
 * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
 * @see get_term_by() You can get a category by name or slug, then pass ID to this function
 * @uses get_term_children() Passes $cats
 * @uses in_category() Passes $_post (can be empty)
 * @version 2.7
 * @link http://codex.wordpress.org/Function_Reference/in_category#Testing_if_a_post_is_in_a_descendant_category
if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    function post_is_in_descendant_category( $cats, $_post = null ) {
        foreach ( (array) $cats as $cat ) {
            // get_term_children() accepts integer ID only
            $descendants = get_term_children( (int) $cat, 'category' );
            if ( $descendants && in_category( $descendants, $_post ) )
                return true;
        return false;

then i create a function to select the correct template based on the parent category which i hook to the single_template hook ex:

add_action( 'single_template', 'post_template_selector' );
function post_template_selector($single_template) {
        global $wp_query;
        global $wp;
        if ( $wp_query->query_vars['post_type'] === 'post' ) {
                if ( have_posts()){
                        //create an array of parent category => template file name ex:
                        $cats_templates = array(
                                '12' => 'post_in_cat_12_file.php',
                                '32' => 'post_in_cat_32_file.php',
                                '56' => 'post_in_cat_56_file.php',
                        foreach ($cats_templates as $cat => $template) {
                                if (in_category($cat) || post_is_in_descendant_category($cat))
                                        return $template;
        return $single_template;            

Mind the $cats_templates array which is where you define a template X for posts under category Y.

share|improve this answer
thank you so much.. i will try this.. but would check my posted image please. i just update that my there.. –  pagol Jun 17 '12 at 3:36
add comment

show all post in their parent category may be work?

$currentCatId = get_query_var('cat');
$currentCatParent = get_term_by('id', $currentCatId ,'category');
$currentCatParentId = $currentCatParent->parent;
$my_query = new WP_Query( 'cat='.$currentCatParentId );
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post();
// Do your stuff
share|improve this answer
add comment

Your Answer


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.