0

I want to create template page on which i could list and display all child categories of my custom category parent! That's the first level, and when those child categories are displayed i would like to link them to a page template where all of the child category posts would be displayed!

Is this doable?

Thanks!

1 Answer 1

0

Yes, this is achievable with the action 'template_redirect'

I've left to write code to find sub-category for yourself!

function my_subcat_template() {

    if ( <is sub category>) {
        include (TEMPLATEPATH . '/category-fun.php');
        exit;
    }
}

add_action('template_redirect', 'my_subcat_template');

Update

This is a better answer for your problem Put this is functions.php

What it does is, it redefines how wordpress handles templates for categories ( and subcategories)

function new_subcategory_hierarchy() {  
    $category = get_queried_object();

$parent_id = $category->category_parent;

$templates = array();

if ( $parent_id == 0 ) {
    // Use default values from get_category_template()
    $templates[] = "category-{$category->slug}.php";
    $templates[] = "category-{$category->term_id}.php";
    $templates[] = 'category.php';      
} else {
    // Create replacement $templates array
    $parent = get_category( $parent_id );

    // Current first
    $templates[] = "subcategory-{$category->slug}.php";
    $templates[] = "subcategory-{$category->term_id}.php";
    $templates[] = "subcategory.php";

    // Parent second
    $templates[] = "category-{$parent->slug}.php";
    $templates[] = "category-{$parent->term_id}.php";
    $templates[] = 'category.php';  
}
    return locate_template( $templates );
}



add_filter( 'category_template', 'new_subcategory_hierarchy' );

Now you can user subcategory-slug.php or subcategory-id.php for specific subcategories

or subcategory.php in general for all subcategories

2
  • Yes, i think i understand, but where should i put this code block? In functions.php? And what should be the condition in if statement exactly? If it's no problem for you to explain it a little more :)
    – Kuka
    Aug 27, 2013 at 12:01
  • I've updated the answer. You can put this code in functions.php or in inside you own plugin. Let me know if this works
    – Dipesh KC
    Aug 27, 2013 at 13:30

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.