WordPress.org

Forums

[resolved] If parent X include page Y? (10 posts)

  1. EntarteteMuzak
    Member
    Posted 6 years ago #

    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?

  2. MichaelH
    Member
    Posted 6 years ago #

    http://codex.wordpress.org/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

  3. EntarteteMuzak
    Member
    Posted 6 years ago #

    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: http://wordpress.org/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');
    ?>
  4. MichaelH
    Member
    Posted 6 years ago #

    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

  5. EntarteteMuzak
    Member
    Posted 6 years ago #

    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.

  6. MichaelH
    Member
    Posted 6 years ago #

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

  7. EntarteteMuzak
    Member
    Posted 6 years ago #

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

  8. EntarteteMuzak
    Member
    Posted 6 years ago #

    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?

  9. MichaelH
    Member
    Posted 6 years ago #

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

  10. EntarteteMuzak
    Member
    Posted 6 years ago #

    Ok

Topic Closed

This topic has been closed to new replies.

About this Topic