Support » Fixing WordPress » If is subcagory. How should I do this?

  • Resolved hilj

    (@hilj)



    Hello,

    I have several categories in my website, one of them is Blog, I would like to format the Blog differently from other categories. I’ve done this using an own template for the Blog.

    But now, when I’m clicking some of the Blog categories (cheese, fruits, what ever) they are rendered from the archive.php template. I would need to write a statement that says: if this category is a subcategory of category “Blog” (ID 8). if (in_category('8')) or if (is_category('8')) is not doing it. Any idea how to do this?

    All help is very much appreciated! Thanks.

Viewing 13 replies - 1 through 13 (of 13 total)
  • Couldn’t you just use the concepts described in Category_Templates?

    Maybe, I’m not sure, could you please explain it a bit? Thanks!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Try copying archive.php to category-8.php and editing that.

    Oh I see, thank you.

    Now it rendering the category 8 from the category-8.php file, but still not the sub categories of category 8.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Subcategories do not automatically use their parent category’s template. The whole subcategory thing is just for sorting of categories and creating breadcrumb trails and such.

    If you want a subcategory to be styled the same, you’ll need to make a category-XX.php for it too. The easy way would be to make one and then simply do an include 'category-8.php'; there, to point it to its parent.

    Alternatively, you could try to be clever about it. Make a category.php file, and put code in it that gets the category number, then gets that cat, then gets the parent, and so on. Something like this:

    category.php

    $cat = get_query_var('cat');
    $temp=array();
    while($cat != 0) {
    $category = get_category($cat);
    $temp[] = 'category-'.$category->parent.'.php';
    $cat = $category->parent;
    }
    $temp[]='archive.php';
    $temp[]='index.php';
    locate_template($temp, true);

    Something like that might work. That’s just shooting from the hip though.

    Now I have 2 categories there, it would be easy to write (is_category('9')) or (is_category('10')). But when the site is in use, there may be tens of categories, every time I want a new category I need to write it in to the template. Would be so easy if I could say: if this is a subcategory of category 8, then do stuff, and not if this is category 9 or 10 or 11 or 12…

    I think I’m approaching this the hard way…

    Thanks for your support so far!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Try that alternative code I just gave you in a category.php file. It might not be 100% right, but I think the principle is sound.

    I did something similar on a clients site – I used some code that would get the category parent and then use the category parent’s info to display the subcategory layout. I gotta dig it up though…let me see if I can find it (it was a while ago when I did it – but it still works great – it was written for 2.0.x and the client just recently upgraded to 2.6.2, and it still works fine.)

    Yep! here it is. Actually, I believe I found this on these forums a long time ago. I think. (It might have even been you, Otto, that I got it from! it would make sense…)

    Just stick this in your functions.php file.

    // inherit template, so that subcategories of a category will base themselves off the parent,
    // and we don't have to go through the whole conditionals thing every time we add a category
    // to be sure the right template is pulled
    
    function inherit_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;
    
    	    }
    	}
    
    }
    
    add_action('template_redirect', 'inherit_template', 1);
    
    // end inherit template

    Hopefully that works! Some of that code looks like it’s kind of old, but it still seems to be working just fine on the client’s site, so it must be okay.

    doodlebee, that’s so nice of you 🙂

    Otto, could you please open up that a bit for me? 🙂

    Thank you! It all seems to be working now!

    Hi dood i have little problems , build simple blog for new car bla bla … all category on my site preset one seperation blog (wp-admin/settings/Prermalinks/ Custom Structure : /mark/%category%/%postname%.html and Category base: mark/ ) problem is a script no work on this , script lock ID for link line $post->ID (script work ok on classic wp permalink http://localhost/?cat=100 ) don’t work on this http://localhost/mark/ford
    i’m use this”
    <?php
    function category_temp() {

    if ( !is_single() || is_404() ) return; // we only care about single posts

    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 . “/mark/category-” . $category->cat=ID. ‘.php’) ) {
    include(TEMPLATEPATH . “/mark/category-” . $category->cat=ID. ‘.php’);
    exit;
    }
    }
    }

    add_action(‘template_redirect’, ‘category_temp’);
    ?>
    Pleas help me 😀

    this seems to be the same problem I am having .. my sub-cats arent picking up the right template.

    I added doodlebees code – but it’s still not working. I think because I’m using the pretty urls.

    helllpppp

    Donna

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘If is subcagory. How should I do this?’ is closed to new replies.