get_depth()
2010-01-16 @ 14:54The problem
As far as I know there is no function in WordPress that return the page or category depth.
The solution
I created a function that returns the depth of a page or category. The depth is how many levels from the root the page or category in its hierarchy. The root level number is 0.
function get_depth($id = '', $depth = '', $i = 0) { global $wpdb; if($depth == '') { if(is_page()) { if($id == '') { global $post; $id = $post->ID; } $depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$id."'"); return get_depth($id, $depth, $i); } elseif(is_category()) { if($id == '') { global $cat; $id = $cat; } $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'"); return get_depth($id, $depth, $i); } elseif(is_single()) { if($id == '') { $category = get_the_category(); $id = $category[0]->cat_ID; } $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$id."'"); return get_depth($id, $depth, $i); } } elseif($depth == '0') { return $i; } elseif(is_single() || is_category()) { $depth = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '".$depth."'"); $i++; return get_depth($id, $depth, $i); } elseif(is_page()) { $depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$depth."'"); $i++; return get_depth($id, $depth, $i); } }
Function call
Send an ID with the function or it will use current ID to get the depth. Call the function somewhere in your theme.
- In a category – It will return the depth of the current category, if no ID is sent.
- In a page – It will return the depth of the current page, if no ID is sent.
- In a post – It will return the depth of the first category assigned to the post, if no ID is sent.
<?php echo get_depth(); ?> <?php echo get_depth(2); ?>
Alternative (for page depth only)
Here comes a much shorter alternative if you only need to get the depth of pages. This works on page.php. If you are using it in other places, make sure the $post variable are correct.
<?php echo count($post->ancestors); ?>
Alternative (for category depth only)
This code works with archive.php or category.php. If you are using another template file, you need to insert the category ID into the $cat variable.
<?php $cats_str = get_category_parents($cat, false, '%#%'); $cats_array = explode('%#%', $cats_str); $cat_depth = sizeof($cats_array)-2; echo $cat_depth; ?>
Contributors
Improvements
Do you have any ideas, bugs, features or anything else to improve the code? Write a comment and I’ll look into it.
2009-09-16 @ 2:24 e m
Thanks for ultra-handy function
I started to write out a much more convoluted way to calculate the same thing until I hit Google and stumbled across your site
2009-09-16 @ 2:25 e m
One way to get the depth of a page is to use the get_post_ancestors() function.
< ?php $ancestors = get_post_ancestors($post); $depth = count($ancestors); ?>
2010-01-17 @ 2:38 e m
It worked! I added you as a contributor to this post. A shorter way of doing it is this:
2011-09-21 @ 2:09 e m
That one liner works great for deep pages, to differentiate front page (0) from level 1 pages (1) see http://xaviesteve.com/2512/wordpress-get-depth-of-pages-and-subpages/
2009-12-19 @ 2:45 e m
LooooooooooL
Thanks a lot for very helpful post !
2010-05-04 @ 3:38 f m
Hey could I reference some of the content here in this post if I reference you with a link back to your site?
2010-05-04 @ 5:45 f m
Yes, I always need backlinks. Thanks for asking.
2010-05-26 @ 7:35 e m
Thank you so much. It’s very difficult to find advanced WordPress snippets like this. I’ll be subscribing.
2010-07-23 @ 10:27 e m
Thanks, saved me a lot of work!!!
2011-04-06 @ 8:26 e m
[…] tutto qui: http://www.devdevote.com/cms/wordpress-hacks/get_depth Tweet Comments […]
2011-05-02 @ 9:43 f m
How failsafe is the function for the depth of category? I mean, because it depends on the ID. I have to make a website with more than a thousand categories: cat > subcats > subsubcats > posts.
2011-05-03 @ 10:32 f m
i think it.s more that ok , i just tested with your kind of depth , cat->subcat->subsubcat->posts and work super duper ok ! i allso used ”Collapsing Categories” plugin to get a nice list of categories in a widget
2011-10-11 @ 6:37 e m
ancestors); ?>
This line of code saved me SO MUCH time! Thank you for sharing!!!
2011-11-16 @ 9:28 e m
[…] http://www.devdevote.com/cms/wordpress-hacks/get_depth […]
2012-01-06 @ 12:55 f m
Hi, great tutorial! I’ve got one question though. I’d like to show sidebar1 for depth1, sidebar2 for depth2 and so on. Any idea how to achieve this?
At this point I’m using for all top level categories:
if is_category(toplevelcat1,toplevelcat2,toplevelcat3…) {
INCLUDE sidebar1
} elseif is_category(2ndlevelcat1,2ndlevelcat2,2ndlevelcat3…) {
INCLUDE sidebar2
} elseif is_category(3rdlevelcat1,3rdlevelcat2,3rdlevelcat3…) {
INCLUDE sidebar3
}
This is pretty complicated when you have 1000 categories. :)
2013-01-27 @ 5:34 e m
I found this blog , “get_depth() – get depth of a page or category – WordPress
template tag / function | devdevote.com”, quite pleasurable and also it
was a good read. Regards,Sonia
2013-07-08 @ 8:10 f m
very good peace of code :D
as for idea i when i was using your code in my theme i face a situation that i have to show categorys only on parent categorys (not last node of category).so i modify your code a bit to check if current category is a parent category or not.here what i ended up with:
if (is_category() ) :
$this_category = get_category($cat);
//get sub categories of current category
$cat_childs = get_categories(array(‘child_of’=>$this_category->cat_ID));
if (sizeof($cat_childs) > 0) : return true;
endif;
endif
hope to be usefull for someone.
endif; …