WordPress.org

Ready to get started?Download WordPress

Forums

do child categories inherit template of parent category? (17 posts)

  1. insidertravels
    Member
    Posted 8 years ago #

    I've been searching endlessly for an answer to this in the codex and forums and everywhere else I could think of on third-party sites catering to WP. I've been so frustrated that I almost gave up and went with another platform several times. I guess I should have asked here sooner, but I didn't want to look silly for asking something so obvious.

    If I have three main categories, all with their own sub-category hierarchies, and I want to have a distinct custom category template for each of the three categories AND their descendent categories, what do I do?

    I thought about trying something like this:


    if(is_category(2) || in_category(2):
    // print layout of category 2
    endif;

    I could also just make a separate category template for each of the three main sections:


    category-2.php
    category-3.php
    category-4.php

    But making separate category templates wouldn't accomplish what I need UNLESS all of the categories nested underneath category-2 would also inherit the template for category-2.

    Do I have to explicitly declare every single sub-category of category-2 (which could be many, many categories and a whole lof of maintenance work)?


    if(is_category(2) || is_category(15) || is_category(56)):
    // and so on, and so on....
    endif;

    I prefer to not install plugins if this can be done with the functions already available to me in WP.

    EDIT: The reason I need this is because I want to avoid having to install multiple copies of WP (or WPMU, for that matter) on my server to accommodate the different page types (i.e. text-based section, image-based section, another section, etc.). I want one all-inclusive website that can share posts between these sections - the only difference between them is how they're displayed on archive pages and which fields are printed to the page.

  2. Michael Bishop

    Posted 8 years ago #

    No need to do any of that. Even though it has a parent category, it's still given a unique cat ID. Template Hierarchy dictates that WP will look for a unique template by ID first. So simply create your templates and name them cat-1, cat-2, cat-3, etc.

    So even though cat-7 is a child of cat-2, it still can have it's own template.

    Category Templates

  3. insidertravels
    Member
    Posted 8 years ago #

    But I want all children of category-2 to share the template for category-2. Is this possible without having to make a separate template for each child?

    If I create category-2.php but do NOT create category-7.php, will the category 7 archive use category-2.php or category.php? Does the template hierarchy honor the category hierarchy?


    category-7.php
    category-2.php
    category.php
    archive.php
    index.php

    I've already read the category templates pages of the codex a million times, but I see no mention of how the template hierarchy deals with the category hierarchy.

  4. Michael Bishop

    Posted 8 years ago #


    distinct custom category template for each of the three categories AND their descendent categories,

    They way I read your OP, I thought you wanted each child to be unique as well as the parents.

  5. insidertravels
    Member
    Posted 8 years ago #

    I already know that is possible. I'm trying to avoid creating a separate template for a bunch of different categories that should all use the same layout. Is this possible?

    If not, would I be able to do something like this in category.php:

    if(!in_category(2)):
    // print all other pages layout
    else:
    include(category-2.php);
    endif;

    Or am I missing some essential part of the logic here?

  6. insidertravels
    Member
    Posted 8 years ago #

    Is there no way to do what I am asking? I'll attempt to clarify my request.

    Example:

    top-level categories:

    category-2
    category-3
    category-4

    subcategories of category-2:

    category-5
    category-8
    category-15
    category-54
    category-100
    etc.

    Can I force all of these subcategories to use the same template as their parent without making a custom template for each of these chilren?

  7. insidertravels
    Member
    Posted 8 years ago #

    OK, nevermind. After what seems like months of exhaustive searching, I think I may have finally found some suitable software to use for my image-based section - we'll see how it goes. So, I'll just use WordPress for my text-based section, then.

    Still, nobody answered my question...it would be nice to know for future reference if the child categories inherit the template used by their parent category. It would make sense if they did, but the template hierarchy makes no mention one way or the other.

  8. aylia
    Member
    Posted 8 years ago #

    I've tried it and found the answer is no, and have to use:
    if(is_category(2) || in_category(2): etc endlessly.

    If you find a better way to do things then I too would like to know it!

  9. yoshi
    Member
    Posted 8 years ago #

    I wanted to do the same thing and made plugin like this.


    add_action('template_redirect', 'inherit_cat_template');

    function inherit_cat_template() {

    if (is_category()) {

    $catid = get_query_var('cat');

    if ( file_exists(TEMPLATEPATH . '/category-' . $catid . '.php') ) {
    include( TEMPLATEPATH . '/category-' . $catid . '.php');
    exit;
    }

    $cat = &get_category($catid);

    $parent = $cat->category_parent;

    while ($parent){
    $cat = &get_category($parent);
    if ( file_exists(TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php') ) {
    include (TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php');
    exit;
    }
    $parent = $cat->category_parent;
    }
    }
    }

    In category page this look for 'category-XX.php' template and if it doesn't exist look for the parent cat's template and so on. And if no category template to the top level cat, then it goes back to normal template hierarchy.

  10. pgiger
    Member
    Posted 8 years ago #

    I'm probably missing something essential in this discussion. Can't you just assaign a post to both main- and sub category? If classical music reviews, for example, belong to both main category "Music" and sub category "Classical"...? And isn't that the obvious way of categorizing posts...? But as I said, I'm probably missing something...

  11. JaneLitte
    Member
    Posted 8 years ago #

    Yoshi - thanks for posting your plugin. I copied the exact text, saved it as a php file and uploaded it to my plugins folder. It works perfectly.

  12. ia
    Member
    Posted 8 years ago #

    I have a follow-up question. What about applying another type of single.php template for every post that falls under a category (in this case for this discussion, category 2 and all its subcategories)? Is that possible? :D

    Yoshi, your plugin is brilliant. I also am using it. Thank you very much. Do you have a web page I can link back to if ever (for the plugin info, at least!)?

  13. moshu
    Member
    Posted 8 years ago #

    http://guff.szub.net/2005/07/21/post-templates-by-category/
    (on the same page another plugin, the "inheritor" is also mentioned!)

  14. ia
    Member
    Posted 8 years ago #

    cool! thanks. ;)

  15. ia
    Member
    Posted 7 years ago #

    Sorry for replying *again*... But I have to ask. Will yoshi's code work with WordPress 2.1? I've used it in my website and now I'm checking whether I should upgrade WP, but I really don't know of this code will work for the latest version of WordPress.

  16. bento
    Member
    Posted 7 years ago #

    Hi Yoshi,

    Thanks for writing the plug-in! This is exactly what I need.
    Unfortunately it doesn't work when I install it.
    This is what I copied into my plug-ins folder:

    <?php
    /*
    Plugin Name: Template Inheritor
    Plugin URI: http://wordpress.org/support/profile/53387
    Description: Chield pages use category template
    Author: Yoshi
    */
    add_action('template_redirect', 'inherit_cat_template');

    function inherit_cat_template() {

    if (is_category()) {

    $catid = get_query_var('cat');

    if ( file_exists(TEMPLATEPATH . '/category-' . $catid . '.php') ) {
    include( TEMPLATEPATH . '/category-' . $catid . '.php');
    exit;
    }

    $cat = &get_category($catid);

    $parent = $cat->category_parent;

    while ($parent){
    $cat = &get_category($parent);
    if ( file_exists(TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php') ) {
    include (TEMPLATEPATH . '/category-' . $cat->cat_ID . '.php');
    exit;
    }
    $parent = $cat->category_parent;
    }
    }
    }
    ?>

    If I don't place a description text at the
    beginning of your script and envelope it with php tags like these:
    "<?php" and "?>" it won't even show up at the plug-ins screen.

    Anyway, now it does show up and can be activated,
    but the child-category (or sub-category) still uses the
    default template, instead of the custom template of
    the parent category.

    I am using the "Post Templates by Category" by
    http://guff.szub.net/ as well. Any conflict there?

    Any advice is greatly appreciated!

  17. yoshi
    Member
    Posted 7 years ago #

    Hi bento,
    They both should work together all right.
    and it works with WP 2.1 too.
    Any other plugins used?

Topic Closed

This topic has been closed to new replies.

About this Topic