WordPress.org

Ready to get started?Download WordPress

Forums

subcategory dynamically matching parent category templates (12 posts)

  1. SocialChris
    Member
    Posted 6 years ago #

    I'm trying to make my sub categories templates match my parent category template dynamically.

    I read the category templates documentation page but that only tells you how to customize each category template page... not how to automatically make the sub categories mimic the parent category template automatically.

    is this possible or do i have to hard code every new category I create category-1.php, category-2.php, category-3.php, etc. ?

    It would be tedious if I add a new sub category then have to go and hardcode a new subcategory template page ... taking category-3.php (the exisiting parent category id) and renaming it to category-4.php (the new subcategory id) then upload via FTP

  2. eyedea
    Member
    Posted 6 years ago #

    Hi, SocialChris

    sorry, I can't give you a solution - looking for one on this myself - just thought I'd post my interest in finding one...

    I'm sure it must be possible - will post if I can work it out ;)

  3. eyedea
    Member
    Posted 6 years ago #

    I've managed to get my setup working now - using the approach mentioned in another post:
    http://wordpress.org/support/topic/110792?replies=2

    hope this might help you as well, SocialChris :)

  4. iPaulPro
    Member
    Posted 5 years ago #

    Hey, SocialChris, come across a solution for this yet?

    Anyone out there?

    Pass Category Template to sub-categories without making each individual category-*.php page?

  5. diesel12
    Member
    Posted 5 years ago #

    In the same boat, surprised this isn't in core functionality of wp....

  6. lilqhgal
    Member
    Posted 5 years ago #

    Also looking for the answer to this!

  7. alainS
    Member
    Posted 5 years ago #

  8. lilqhgal
    Member
    Posted 5 years ago #

    I need this to work for categories, not pages, although this plugin is nice to know about. Thanks.

  9. johpg
    Member
    Posted 5 years ago #

    I think the thread at http://wordpress.org/support/topic/127550 has the answer, but I can't make it work for me.

    You put this in functions.php to work out whether you are in a parent category or not:

    function get_parent_category() {
     global $wp_query;
     $category = $wp_query->get_queried_object();
     if ($category->category_parent) return $category->category_parent;
    }

    Then you add this to your archive.php:

    <?php $id = get_parent_category();
     if ($id) {
     include(TEMPLATEPATH . '/template' . $id . '.php');
    } else {
     include(TEMPLATEPATH . '/a_different_template.php');
     } ?>

    However, this isn't working for me - I'm assuming I'm not putting the code in the right place.
    Where exactly in archive.php do you put the code?
    Or maybe it doesn't go in archive.php, but somewhere else?

  10. emmi
    Member
    Posted 5 years ago #

    Hi!
    I've also been trying and trying to solve the same thing. Then I did find a way to make it work.
    When putting this code in category.php (only this code, nothing else) it takes all the childcategories of category-3 to one template and all childcategories of category-4 to another.

    <?php
    $category = $wp_query->get_queried_object();
    if ( $category->category_parent ) {
    	$cat_parent = $wpdb->get_var("SELECT name from $wpdb->terms WHERE term_id = $category->category_parent");
    }
     ?>
     <?php if ($category->category_parent == 3) {
     	include(TEMPLATEPATH . '/first_categorytemplate.php');
    }elseif ($category->category_parent == 4) {
    	include(TEMPLATEPATH . '/second_categorytemplate.php');
    	 } else {
    
     include(TEMPLATEPATH . '/categorytemplate_for_everything_else.php');
     }
     ?>

    I was so happy until I tried to do the same thing on single.php and found out it does not seem to work on single posts.

    Have to keep on trying I guess (each of my parent categories has about 25-35 subcategories so writing them down one by one just seem to much)
    When I get time I'll also have a look at the code you've posted johpg, and se if I can get it to work, or if it perhaps works better.

  11. csoul
    Member
    Posted 5 years ago #

    Here's a way to make single posts display in the parent category template. I used the Force Category Template plugin, but modified it as follows:

    foreach ( $categories as $category ) {
    		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; }
    	}

    The if section is from the original plugin; the elseif section is the modification.

    Now all I have to do is figure out some similar way to format the category page. I suspect that will mean adding a similar switch to the template hierarchy code in wordpress, if I can find it, or figuring out how to write my own plugin.

    Best of luck,

    cSoul

  12. csoul
    Member
    Posted 5 years ago #

    Here's the modified plugin (this is the full code) which will force subcategory pages and individual subcategory posts to assume the template of the parent category. (Man, I can't believe I missed this one the first time though...)

    <?php
    /*
    Plugin Name: Force Category Template
    Plugin URI: http://txfx.net/code/wordpress/force-category-template/
    Description:
    Author: Mark Jaquith
    Version: 0.1
    Author URI: http://txfx.net/
    */
    
    /*
    Edited to force category pages and single posts to assume the parent category template in the absence of a unique child-category template by Jason, Screaming Light Studios, August 2008. */ 
    
    /* GPL goes here */
    
    function txfx_force_category_template() {
    
       if ( is_category() || is_single() || !is_404() )  // sort for single posts or categories, but under no circumstances do this for the 404 page!
       {
    	global $posts, $post;
    
    	$post = $posts[0];
    	$categories = get_the_category($post->ID);
    
    	if ( !count($categories) ) return; // no category with which to work
    
    	foreach ( $categories as $category ) {
    		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; }
    	}
       }
       else return;
    }
    
    add_action('template_redirect', 'txfx_force_category_template');
    ?>

    cSoul

Topic Closed

This topic has been closed to new replies.

About this Topic