Support » Themes and Templates » Using in_category for all children categories

  • Hello all

    I have a large site with nuerous categories and multiple templates. In my single.php file I have a lot of IFs and ELSEIFs which check for categories.

    <?php if ( in_category('X') ) {
     include(TEMPLATEPATH . '/this_template.php');
     } else {
     include(TEMPLATEPATH . '/a_different_template.php');
     } ?>

    This has worked fine to a degree. However I want to be able to make all posts that are in the child category of X call a specific template file. So using the above example, if a post was in category Y that was a child of category X I would still want it to include the this_template.php file.

    I hope this is achievable – any help much appreciated.

    Cheers
    Matt

Viewing 9 replies - 1 through 9 (of 9 total)
  • I wrote a small function for another reason, but maybe it works for you as well. Add that to your functions.php:

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

    If a category has a parent, it returns the parent ID, if not, just the ID. In your single.php, you’d have to assign that to another variable and use it with in_category:

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

    Hi Sivar

    Thanks for that – I can certainly see the logic in how that would work. however I do have one further question – I have many categories and sub categories. Does this method allow me to pick and choose which ones with parents I am able to apply different templates to, or does this just say ‘anything with parent, no matter which parent, do this, otherwise do this’?

    If so is there a way of tweaking it to say ‘if parent is $this then do this, else if parent is $this, do something else, otherwise do this’

    Thanks
    Matt

    Hmm, i think, there was an error in my logic, because the function will always return an ID, so the code in single.php will always call this_template. Maybe you need the function modified that it only returns a value, if a category has a parent. The rest will be in single.php. Try this one:

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

    and in your single.php…

    <?php $id = get_parent_category();
     if ($id == 10) {
     include(TEMPLATEPATH . '/this_template.php');
     } elseif ($id == 11) {
     include(TEMPLATEPATH . '/that_template.php'); // and so on
    } else {
     include(TEMPLATEPATH . '/a_different_template.php');
     } ?>

    An even nicer way would be to name the templates "template10.php", "template11.php" and so on, and integrate the value of $id directly, or use a_different_template if there is no parent category:

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

    Of course, you’d have to amend the parent category IDs in the filenames (template10.php, template11.php, etc) to your needs. Good luck! 🙂

    Hi Sivar

    I think the theory is now perfect, but there seems to be a problem with the function returning the parent category. It currently returns a seemingly random value, of no relevance to the actual post.

    The template idea is certainly an interesting one – i might have a look at that if we can get this function working.

    Many thanks for any more help you can provide.

    Matt

    Hmm, my guess is, that category_parent isn’t part of the query if one calls a single post… try this one, but I haven’t tested it:

    function get_parent_category() {
     foreach ((get_the_category()) as $cat) {
     if ($cat->category_parent) return $cat->category_parent; }
    }

    Eventually, this won’t work as expected if a post is in more than one child category. In that case, I think it will return the parent category from the highest child category ID. If the post is in one child category AND one “normal” category, the function should return the child’s parent category ID. If the post is in no child category, the function shouldn’t return anything.

    You can leave the code in your single.php as it is now.

    I found this thread very useful. Sivar’s function was missing the last line which seemed to fix the problem I was having.

    function get_parent_category() {
     foreach ((get_the_category()) as $cat) {
     if ($cat->category_parent) return $cat->category_parent;
    else return $cat->cat_ID;}
    }
    richwillars

    (@richwillars)

    Studying the above functions and trying to get them to work, when I was in a sub category, most of the time it returned 0. I believe this is because of the else statement, which is stopping the for loop from looping.

    Anyway, I wrote my own function… it returns the parent category ID for any category down to one level.

    function get_parent_category() {
    	global $cat;
    	foreach ((get_the_category()) as $cato) {
    		if($cat==$cato->cat_ID) {
    			if($cato->category_parent==0)
    				return $cat;
    			else
    				return $cato->category_parent;
    		}
    	}
    	return $cat;
    }

    Hi,
    Sivar, thanks for the get_parent_category method – its just what I was looking for. I’m doing a similar thing as the_fear66 and I am using a similar block of code as given in the example, however it is much more efficient to use a switch statement:

    <?php
    $id = get_parent_category();
    switch($id)
    {
      case 10:
        include(TEMPLATEPATH . '/this_template.php');
        break;
      case 11:
        include(TEMPLATEPATH . '/that_template.php');
        break;
      // and so on
      default:
        //the default template
        include(TEMPLATEPATH . '/a_different_template.php');
        break;
     }
    ?>

    Hope that helps someone.

    Thanks again for the method!

    Lonewolf

    I can’t get this to work:

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

    Exactly which file, and in what position in the file, does the code go?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Using in_category for all children categories’ is closed to new replies.