WordPress.org

Ready to get started?Download WordPress

Forums

Category Template Based on Category Level (9 posts)

  1. mikedev
    Member
    Posted 4 years ago #

    I have a site with a category structure that is large, deep, and dynamic. I want to use a category template based on a conditional statement that uses the current category depth level.

    I don't want to use is_category('13,14,15') because with hundreds of categories, that may be added and removed, I don't want to hard code every category ID in a certain level.

    In an ideal world, this would be my code (unfortunately there is no such conditional statement):

    <?php
    	if ( is_category_level('1') ) {
    		include(TEMPLATEPATH . '/category-level-1.php');
    	} elseif ( is_category_level('2') ) {
    		include(TEMPLATEPATH . '/category-level-2.php');
    	} elseif ( is_category_level('3') ) {
    		include(TEMPLATEPATH . '/category-level-3.php');
    	} else {
    		include(TEMPLATEPATH . '/category.php');
    	}
    ?>

    Here are two forum posts that might be similar: http://wordpress.org/support/topic/369287?replies=4, and http://wordpress.org/support/topic/338561?replies=8.

    Can anyone help me figure out how to specify the category template based on category level?

    Thank you!

  2. esmi
    Forum Moderator
    Posted 4 years ago #

    You might find the cat_is_ancestor_of useful.

  3. keesiemeijer
    moderator
    Posted 4 years ago #

    put this in your theme's functions.php

    function is_category_level($depth){
    $current_category = get_query_var('cat');
    $my_category  = get_categories('include='.$current_category);
    $cat_depth=0;
    
     if ($my_category[0]->category_parent == 0){
     	$cat_depth = 0;
     	} else {
    
     while( $my_category[0]->category_parent != 0 ) {
      $my_category = get_categories('include='.$my_category[0]->category_parent);
      $cat_depth++;
      }
      }
      if ($cat_depth == intval($depth)) { return true; }
      return null;
    }

    and use the function in category.php like this:

    <?php
    if(is_category_level('1')){
    	// do stuff
    }
     ?>

    is_category_level('0') = parent-category (not a sub-category)
    is_category_level('1') = a sub-category of a parent-category
    is_category_level('2') = sub-category of a sub-category of a parent-category
    etc.. etc..

  4. mikedev
    Member
    Posted 4 years ago #

    keesiemeijer,

    Thank you very much for the function. Unfortunately it always only returns level 0 no matter what level of category is visited. Is there something I need to change to recognize all levels?

    Thanks!

  5. keesiemeijer
    moderator
    Posted 4 years ago #

    I just copy pasted the code here on my server and it works. Are you using the function in category.php?

  6. mikedev
    Member
    Posted 4 years ago #

    keesiemeijer,

    After a bit of testing I found it only works when there is a post directly in the category. In my site structure the posts are only in the third-level category.

    Is there anything I can change in the function to make it work even if there is no post in the category?

    Thank you for your continued help!

  7. keesiemeijer
    moderator
    Posted 4 years ago #

    try changing these lines in the function:
    $my_category = get_categories('include='.$current_category);
    and
    $my_category = get_categories('include='.$my_category[0]->category_parent);

    to:
    $my_category = get_categories('hide_empty=0&include='.$current_category);
    and
    $my_category = get_categories('hide_empty=0&include='.$my_category[0]->category_parent);

  8. mikedev
    Member
    Posted 4 years ago #

    keesiemeijer,

    Works!

    You are generous and truly awesome!

    Thank you very much!

  9. keesiemeijer
    moderator
    Posted 4 years ago #

    Glad it is working! Now, please use the dropdown at top right to mark this topic 'Resolved'.

Topic Closed

This topic has been closed to new replies.

About this Topic