2

Let me tell you the scenario first say the structure of the categories in wordpress is like this

Level 1: Top
Level 2: -Nextme_1
Level 3: --Nextme_2
         --Nextme_3
Level 4: ---Nextme_4
         ---Nextme_5

Now I require to check what is the level of the category? Say I catch a category of level 3 so I have to use different template and if its level 4. Then I need to use another template?

Anybody can give me some hint?

Thanks
Rahul

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
Rahul
  • 364
  • 3
  • 8
  • 21
  • About to write a procedure for this in CSV 2 POST. The solution giving looks great but I think I'll use a while loop and return once parent reached. – Ryan Bayne Mar 19 '14 at 14:27

4 Answers4

7

If you don't have many categories you can try to edit their slug from admin, and then in your page you get the category slug this way:

if (is_category()) {
    $cat = get_query_var('cat');
    $category = get_category($cat);
    echo 'your slug is '. $category->slug;
}

Now, when you're editing the categories slugs try naming them after their level: cat-lvl-1, cat-lvl-2. Then in your page you extract the number from category slug using some php string function, and then you check that number:

if ($category->slug == 1 ) {
//load the template for the category of level 1
}
 if ($category->slug == 2 ) {
    //load the template for the category of level 2
    }

and so on.

Later edit: Try this:

function get_level($category, $level = 0)
{
    if ($category->category_parent == 0) {
        return $level;
    } else {
        $level++;
        $category = get_category($category->category_parent);
        get_level($category, $level);
    }

}

if (is_category()) {
    $cat = get_query_var('cat');
    $yourcat = get_category($cat);

    echo get_level($yourcat);
}
zuzuleinen
  • 2,604
  • 5
  • 38
  • 49
4

You can call the get_ancestors() function to get an array containing the parents of the given object. Then you need to count elements in the result.

function get_the_level($id, $type = 'category') {
  return count( get_ancestors($id, $type) );
}

if( is_category() ) {
  $level = get_the_level( $cat );
} 
elseif( is_product_category() ) {
  $level = get_the_level( $wp_query->get_queried_object()->term_id, 'product_cat' );
}
Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
3

Thanks a lot. This is superb with slight a change the code that you have written is fine but its not returning any value.(i,e the $level) although its calculating correct. A minor change i did and its work fine now with a slight editing of you code given below..

`

function get_level($category, $level = 0)
{
    if ($category->category_parent == 0) {
        return $level;
    } else {
        $level++;
        $category = get_category($category->category_parent);
        return get_level($category, $level);
    }

}

if (is_category()) {
    $cat = get_query_var('cat');
    $yourcat = get_category($cat);

    echo get_level($yourcat);
}

`

Thanks @zuzuleinen

Rahul
  • 364
  • 3
  • 8
  • 21
0

I visited this page months back. I came back today, arrow up on the above solution then still went digging. Although it is a good solution, Wordpress often offers better or close.

get_category_parents()

This function does as Rahul has typed basically. It also calls itself which seems the most logical approach and that is why Rahul gets a point from me on this. Do not use $link, return a string of categories, explode() them then count or I suppose we could count the number of times the separator has been used and add 1.

function get_category_parents( $id, $link = false, $separator = '/', $nicename = false, $visited = array() ) {
   $chain = '';
$parent = get_term( $id, 'category' );
if ( is_wp_error( $parent ) )
    return $parent;

if ( $nicename )
    $name = $parent->slug;
else
    $name = $parent->name;

if ( $parent->parent && ( $parent->parent != $parent->term_id ) && !in_array( $parent->parent, $visited ) ) {
    $visited[] = $parent->parent;
    $chain .= get_category_parents( $parent->parent, $link, $separator, $nicename, $visited );
}

if ( $link )
    $chain .= '<a href="' . esc_url( get_category_link( $parent->term_id ) ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $parent->name ) ) . '">'.$name.'</a>' . $separator;
else
    $chain .= $name.$separator;
return $chain;

}

Ryan Bayne
  • 162
  • 1
  • 10