tags:

views:

20

answers:

1

I want a post have the template of the parent category.. Is that possible? if yes, please guide me a bit. Or if any plugin is available, name it.

+2  A: 

As of Wordpress 3.0, the logic in wp-includes/template-loader.php for selecting a template looks like this:

if ( defined('WP_USE_THEMES') && WP_USE_THEMES ) :
    $template = false;
    if     ( is_404()            && $template = get_404_template()            ) :
    elseif ( is_search()         && $template = get_search_template()         ) :
    elseif ( is_tax()            && $template = get_taxonomy_template()       ) :
    elseif ( is_front_page()     && $template = get_front_page_template()     ) :
    elseif ( is_home()           && $template = get_home_template()           ) :
    elseif ( is_attachment()     && $template = get_attachment_template()     ) :
        remove_filter('the_content', 'prepend_attachment');
    elseif ( is_single()         && $template = get_single_template()         ) :
    elseif ( is_page()           && $template = get_page_template()           ) :
    elseif ( is_category()       && $template = get_category_template()       ) :
    elseif ( is_tag()            && $template = get_tag_template()            ) :
    elseif ( is_author()         && $template = get_author_template()         ) :
    elseif ( is_date()           && $template = get_date_template()           ) :
    elseif ( is_archive()        && $template = get_archive_template()        ) :
    elseif ( is_comments_popup() && $template = get_comments_popup_template() ) :
    elseif ( is_paged()          && $template = get_paged_template()          ) :
    else :
        $template = get_index_template();
    endif;
    if ( $template = apply_filters( 'template_include', $template ) )
        include( $template );
    return;
endif;

Checking get_category_template() in wp-includes/theme.php` we see:

function get_category_template() {
    $cat_ID = absint( get_query_var('cat') );
    $category = get_category( $cat_ID );

    $templates = array();

    if ( !is_wp_error($category) )
        $templates[] = "category-{$category->slug}.php";

    $templates[] = "category-$cat_ID.php";
    $templates[] = "category.php";

    $template = locate_template($templates);
    return apply_filters('category_template', $template);
}

Assuming that your category is Foo, that it's slug is foo, and that the Foo category ID is 17, for a post that belongs to category Foo, Wordpress will check for the following templates in your theme and use the first one it finds:

  • category-foo.php
  • category-17.php
  • category.php

Thus, all you should need to do is to create a template named category-foo.php in your theme directory, and set your post's category to Foo, and that post will be rendered using the category-foo.php template instead of the default post.php template.

This mechanism for selecting templates has been present since Wordpress 1.5, though full list of template types has grown significantly over the years.

The Wordpress documentation for this can be found here.

Craig Trader
My permalink is set to IDs.. So each category is referred with its ID. I've already made a category file for a category "real-estate" with id 3 as category-3.php, but it aint being applied on a post with the category "real-estate"..
Bibhas
The permalink format has nothing to do with the mechanism for selecting templates. I've given you where to look to debug this, but you're going to have to do the looking, since I don't know (a) what version of Wordpress you're using, (b) what theme you're using, (c) what plugins you've installed, or anything else. If I had to guess, your post category ID isn't really 3 -- rename the template to category-real-estate.php, and make sure it's readable by the webserver.
Craig Trader
The category id is 3 alright. Anyway, if the posts are Supposed to have the template of the parent category, and there's nothing else to it, then I'll see about it.But tell me, what'll happen for a post that has multiple category?
Bibhas
I think what you should do is insert a debugging statement like `echo "<!-- template is $template --\n"` after the inner `endif;` in `template-loader.php` so you know which template is being selected. It may be the case that a different template is being selected before testing for a category template.
Craig Trader
Thanx Craig. Will check it out.
Bibhas