Support » Themes and Templates » [Resolved] If parent X include page Y?

[Resolved] If parent X include page Y?

  • 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)
  • 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


    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.

    Plugin Name: Child Force Category Template
    Plugin URI: http://wordpress.org/support/topic/234874
    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.

    Category Templates
    Template Hierarchy

    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.

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

    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.


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