• Resolved EntarteteMuzak

    (@entartetemuzak)


    I’m using WP to build a site with 50+ categories for a university campus. They would like to be able to create new categories using different looks.
    The ideal thing would be to be able to choose which template to use, like with pages, but since that is not possible out of the box (and I haven’t found any plugins that will enable me to do it either) my last resort is to write a snippet of code that will check parent category and then based on the result use a specific template.

    I have 8 main categories (parents), each assigned with a different look.
    I would like each subcategory (child) to be given the same as the parent automatically. Is there a way to do this?

Viewing 9 replies - 1 through 9 (of 9 total)
  • https://codex.www.ads-software.com/User:MichaelH/MyPlugins has reference to Plugin Name: Child Force Category Template that should do you…

    Works for Parent/Child but not Parent/Child/Grandchild

    Thread Starter EntarteteMuzak

    (@entartetemuzak)

    Thanks.

    So where do I put parent name/id?
    (not that good with scripts)

    I tried embedding it in archive.php, the I created a file named category-about.php containing my category template (parent category is named ‘About’). I also tried using the id for parent category to form the name (category-3.php).
    No results.

    <?php
    /*
    Plugin Name: Child Force Category Template
    Plugin URI: https://www.ads-software.com/support/topic/234874
    Description:
    Author: MichaelH modified original from Mark Jaquith
    Version: 0.0
    Author URI:
    */
    
    /* GPL goes here */
    
    function child_force_category_template() {
    
        if ( !is_category() || is_404() ) return; // we only care about category views
    
        $cat = get_query_var('cat');
        $category = get_category ($cat);
    
        if ( !($category) ) return; // no category with which to work
    
            if ( file_exists(TEMPLATEPATH . '/category-' . $category->cat_ID . '.php') ) {
                include(TEMPLATEPATH . '/category-' . $category ->cat_ID . '.php');
                exit; }
            elseif ( file_exists(TEMPLATEPATH . '/category-' . $category->category_parent . '.php') ) {
                include(TEMPLATEPATH . '/category-' . $category->category_parent . '.php');
                exit; }
    }
    add_action('template_redirect', 'child_force_category_template');
    ?>

    You don’t. This assumes if your Parent is category id = 15 then you have a template in your theme file called category-15.php that will be used to display posts that belong to category 15 and any ‘child’ categories of category 15.

    See
    Category Templates
    Template Hierarchy

    Thread Starter EntarteteMuzak

    (@entartetemuzak)

    Tried that, didn’t work.

    If my parent is id 3 I create a file called category-3.php and embedd the code I want to display.
    I put the Child Force Category Template in archive.php and remove everything else for that file.

    And…… nothing. Blank.

    No, you save the Child Force Category as a plugin, in wp-content/plugins folder, then activate the plugin.

    Thread Starter EntarteteMuzak

    (@entartetemuzak)

    Oh… (blush).
    It works.
    Thank you for your patience.

    Thread Starter EntarteteMuzak

    (@entartetemuzak)

    One last question.
    If I want to bypass the plugin on one page is it enough to just create a category page (say category-15.php) for that category?

    All the plugin does is force the template for a child category to use the template of the parent category.

    Thread Starter EntarteteMuzak

    (@entartetemuzak)

    Ok

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘If parent X include page Y?’ is closed to new replies.