Get Depth – like is_child() & is_grandchild()
September 7th, 2008Is there a function in WordPress to check the current page depth like is_child(), is_grandchild() or a number for unlimited depth? I could’nt find any so I created one. It is a recursive function which means that it calls itself until it’s done.
Put the following code to the functions.php in your WordPress theme folder.
function wt_get_depth($id = '', $depth = '', $i = 0)
{
global $wpdb;
global $post;
if($depth == '')
{
if(is_page())
{
if($id == '')
{
$id = $post->ID;
}
$depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$id."'");
return wt_get_depth($id, $depth, $i);
}
}
elseif($depth == "0")
{
return $i;
}
else
{
$depth = $wpdb->get_var("SELECT post_parent FROM $wpdb->posts WHERE ID = '".$depth."'");
$i++;
return wt_get_depth($id, $depth, $i);
}
}
Call the function like this in your theme:
<?php echo wt_get_depth(); ?>
or if you want to choose a custom page:
<?php echo wt_get_depth(3); ?>
Here is a way do use the function:
<?php if (wt_get_depth() == 2)
{
echo "This page is a grand grandchild";
}
?>
If you need a function like is_child() or is_grandchild(), create an is_child() function like this:
function wt_is_child($id = '')
{
if(_get_depth($id)>0)
{
return true;
}
}
or the is_grandchild() function:
function wt_is_grandchild($id = '')
{
if(_get_depth($id)>1)
{
return true;
}
}
and call them like this:
<?php if (wt_is_child())
{
echo "This is a child page";
}
?>
or
<?php if (wt_is_grandchild())
{
echo "This is a grandchild page";
}
?>
To get all new functions/template tags, install my plugin WP Extra Template Tags.
I think you can solve all this easily using get_ancestors().
$depth = count(get_ancestors($post, ‘post’));
Thanks so much for this – saved me hours!
Cheers BigG
thanks so much ! my friend ! i copy it and you will see your copyright : http://muasamvui.com/f/get-depth-of-the-menu-like-is-child-is-grandchild-t3226.html
Wow, this really helped me out a lot..thanks!!!
To return just the level below the current page you need this:
get_pages(‘child_of=’.$post->ID.’&parent=’.$post->ID);
foreach ($pages as $page) {
//something in here
}
Hope this helps
Hi!
If you would like too get a different echo for each parent/grandparent? How would you do that and where should i put the code?
My page is : http://trollback.se/urskog/products/lov/walnut/
So I want headerMeny to be highlighted and “lov” when on a subpage to them. So how should i code this?
This is a extract from my current meny:
<li class="mainNav_Lov">
<a href="/products/lov/walnut”>
THIS IS MY HEADER MENY:
<li class="products">
<a href="/products/lov/walnut”>
Basicly I want to get “LÖV” highlighed with : id = current2
and I want Products to be hlighlighted : id= current
Thank you!
Thanks for the function! Saved me a headache.
hey, thank you for the code. i was looking for a code to check the depth of a category, i tweaked your code and it helped solve a problem i was trying to solve.big up.
here is the code to get the depth of categories;
function wt_get_depth($id = ”, $depth = ”, $i = 0)
{
global $wpdb;
global $term_taxonomy;
if($depth == ”)
{
if(is_category())
{
if($id == ”)
{
$id = $term_taxonomy->term_id;
}
$depth = $wpdb->get_var(“SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = ‘”.$id.”‘”);
return wt_get_depth($id, $depth, $i);
}
}
elseif($depth == “0″)
{
return $i;
}
else
{
$depth = $wpdb->get_var(“SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = ‘”.$depth.”‘”);
$i++;
return wt_get_depth($id, $depth, $i);
}
}
Copied the wrong code… Oops.
the line finding the parent of the post should be:
if ($post->post_parent) $parent = $post->post_parent;
not:
if ($post->post_parent) $parent = get_page($post->post_parent);
I had been searching for an answer to the ‘is_grandchild_of(idhere)’ idea myself.
This is what I came up with as a modification of the is_tree() idea mentioned in the codex but only checks a specific number of levels. My next step would be to combine it with your get_depth idea:
function is_tree($pid) {
global $post;
if ($post->post_parent) $parent = get_page($post->post_parent);
if ($parent->post_parent) $grand = get_page($parent->post_parent);
if ($grand->post_parent) $great = get_page($grand->post_parent);
if ($great->post_parent) $gr8gr8 = get_page($great->post_parent);
if (is_page($pid) || $parent==$pid || $grand->ID==$pid || $great->ID==$pid || $gr8gr8->ID==$pid) :
return true; // Yes, it’s in the tree
else : return false; // No, it’s outside
endif;
Hi there,
Perhaps this is similar to Jonathan’s query, but I’m almost losing track of what I’m looking for after having visited so many pages!
On a page, I’m trying to loop out the grand children of the page I’m at, grouping them by child – is that possible with this code? I’ve got a template for a specific page, where I’d like to get the title of each child and underneath each child list page data from the grandchildren. Something like
CHILD 1 TITLE
- Grandchild 1 with title and description
- Grandchild 2 with title and description
CHILD 2 TITLE
- Grandchild 1 with title and description
- Grandchild 2 with title and description
Any pointers on this would be great, ideally a code example to help a feeble mind!
hi
very useful code. however, i’d like to have something like
is_grand_child_of(page_id_here)
….
is it possible with this code ?
thanks
hey can you conceive of a get_depth for CATEGORIES (ie i want to use get_categories but only display one depth, not all the subchildren)? i know wp_list_categories does this, but it echoes a list, not generate an array like get_categories…
thanks!