Check if a Page is a Child of Another Page in WordPress
by c.bavota | Posted in Tutorials | 23 comments
This post has been updated in “is_child() Conditional Function for WordPress”.
WordPress has some default conditional tags that are really helpful when developing themes and plugins that need specific functions on specific pages. Strange enough though, there is no way to check if a page is a child of another page.
This small function checks the database to see if the page happens to have a parent page assigned to it. If so, then the function will return true.
function is_child($pageID) {
global $post;
if( is_page() && ($post->post_parent==$pageID) ) {
return true;
} else {
return false;
}
}
Place the above code in your theme’s functions.php file, or create a functions.php file and place it in your theme’s directory. Then you can use the is_child() function anywhere in your theme. Your code would look something like this:
<?php
if(is_child(343)) {
echo "This is a child page of 'The Parent Page Title'.";
}
?>



This rocks, thanks a lot!
muy bueno exelente!!
It’s great to know your way around WordPress. I love it! Even if you do have to do some things in a roundabout way.
This is so simply brilliant, I’m surprised its not already a default function in wordpress. Thanks for posting this. For a non php person, its sort of like an ah-ha moment.
Good code but note it would appear that if a page is a ‘child of a child’ it will return false NOT true as I expected
NB I had this problem ‘only works one level deep’
But this function works with all ‘ansistors’ not just single level ‘child’ pages
***also take straight from the WP codex***
http://codex.wordpress.org/Conditional_Tags
function is_tree($pid) { // $pid = The ID of the page we’re looking for pages underneath
global $post; // load details about this page
$anc = get_post_ancestors( $post->ID );
foreach($anc as $ancestor) {
if(is_page() && $ancestor == $pid) {
return true;
}
}
if(is_page()&&(is_page($pid)))
return true; // we’re at the page or at a sub page
else
return false; // we’re elsewhere
};
Steve – your addition is excellent. I’ve used a few of these, but this is one that works as promised…
Ok i really want to get these kind of codes but I need some sort of tutorial to get the logic.
Im not seeing the benefit of what it can do for you, is there some working demo or example i can take a look at? please let me know via email, cheers.
Thanks so much for this post and the ‘is_tree’ function to test whether a wordpress page is a descendant of (child of, grandchild of, great-grandchild of) a particular page id. Brilliant!
Thanks for this. Just made my day and saved me from having to update the site once it’s handed over.
Cheers!
Can you please let me know why it is important to know if a page is a descendant of another page, and where in the theme you would see this at work? Thanks from a php newbie…I’m learning!
I agree with him. We do have the same question. Hope we can have your reply..Thanks for sharing it especially to newbies (like me).
I have used this in the past to create a different page template for all child pages of a specific parent. It just gives more control over how you can design your pages if they are a child page.
Thank you so much for this! I was working on a client’s WordPress blog and I needed to find this information out. I tried your code and it worked. Thanks!!!
At last, I’m clarified. Thanks for the information! I will certainly use it.
Thanks for the code. I need this to check all my sites for child pages.
Thank you so much. You have no idea how much time you saved me.
Thanks for this! It is awesome…I will be using it.
I’m having problems with subdomains pointing to the primary domain. I have had the hosting company deal with this twice with no success. Is it possible that somehow my page either fathered or mothered another page? How do I tell?
Simple and clean way of doing it.
I’ve used Steve’s way of doing it which looks more simple.
Good day man!