Here’s a handy WordPress function I put together today. It tells you if the current page is a child of another page. I’m using it to show a sub-navigation on a page and its children.
function is_child ($parent) {
global $wp_query;
if ($wp_query->post->post_parent == $parent) {
$return = true;
}
else {
$return = false;
}
return $return;
}

June 1st, 2007 at 3:11 pm
Could this be used in an if statement to not display child pages?
ex:
if(is_child(’parentpageidhere’))
I’m looking for something that will not display child pages if you are currently on a child page of a specific parent page.
June 1st, 2007 at 3:14 pm
if you didn’t want to display child pages, you could simply say:
if (!is_child(’parentpageid’)) {
//code here
}
The key thing is ! (not) tells you to only do the code when is_child returns false.
June 1st, 2007 at 3:46 pm
Thanks!
I’ve been looking for something like this all day :S, and this function works great!
And thanks for responding so quickly.. 3 minutes.
Kudos Nicholas!
June 2nd, 2007 at 11:46 am
No problem Mike. I just realized that this function will only detect direct children and not grandchildren (if that makes sense). Basically, you can have a page that’s a child of a child of a child. Anyway,I might fix that in the future, but it will only complicate it. The function would probably need to be recursive or something …
September 7th, 2008 at 1:28 pm
Hi there!
Short and nice function!
I’ve written a bit more advanced function to be able to get the grand grand grand grand child if that is needed (unlimited).
Have a look at it if you like at Get Depth - like is_child() & is_grandchild() at web-templates.nu
Regards Jens Törnell
September 12th, 2008 at 8:58 pm
I’m sort of wringing my hands here - looking for some advice. Is there a way to check if a page has ANY children? As it stands, it seems even first-level pages with no children are considered parent-pages. Basically, I want to display secondary navigation if and only if a page is a parent to an actual child page, or is a child page. If there are no children, then there should be no special secondary navigation area. Ideas?
September 14th, 2008 at 7:30 am
Hi Will,
I think there’s a pretty simple solution to your problem. Just use wp_list_categories. It will return a list of categories and children categories will be nested under their parents.
November 18th, 2008 at 7:24 am
Hi Nic,
Fantastic little function. I coded up one very similar, my problem is that I need to check whether a specific category has a specific parent, such as..
is_child(bmw,cars);
If bmw is a child of parent, cars, it will return true.
What do you think?
November 19th, 2008 at 3:56 pm
Hey, chris, my function works inside the loop were a specific page has already been pulled from the database. In your case, it sounds like you want to make a function that works outside of the loop, which should be easy enough. You’ll just have to query the db for bmw and grab it’s parent … You could even make it recursive, that way. One caveat, wordpress’s db structure has changed a lot since I wrote this.
May 4th, 2009 at 9:29 pm
[...] the conditional in your theme(s), add the code above to functions.php. Special thanks goes out to Nicholas Roussos for creating this wonderful [...]
January 18th, 2010 at 2:17 pm
[...] Nicholas Roussos – is_child function [...]