Nov
20
2009

Check if a Page is a Child of Another Page in WordPress

by   |  Posted in Tutorials  |  20 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'.";
}
?>

About the author:

A freelance web developer living in Montreal who spends most of his time writing for this site and building Premium themes for WordPress. You can find him on Twitter @bavotasan.

Site5 Affiliate Link
If you liked this, please share it.

Tags: , , , , , , , , , , , , ,

Short URL: http://bit.ly/c4pYxR

Discussion 20 Comments

  1. Matt Cassarino on February 26, 2010 at 12:26 pm

    This rocks, thanks a lot!

    • el loco on August 2, 2010 at 1:50 am

      muy bueno exelente!!

  2. Acai on April 19, 2010 at 8:54 am

    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.

  3. Trisha on October 10, 2010 at 6:06 am

    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.

  4. steve on November 30, 2010 at 2:07 pm

    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

    • steve on November 30, 2010 at 2:21 pm

      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
      };

    • Matt Willson on December 22, 2010 at 9:23 am

      Steve – your addition is excellent. I’ve used a few of these, but this is one that works as promised…

  5. Rams on December 8, 2010 at 8:03 am

    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.

  6. Steve S. on January 5, 2011 at 6:16 pm

    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!

  7. Booker Q on February 7, 2011 at 5:30 pm

    Thanks for this. Just made my day and saved me from having to update the site once it’s handed over.
    Cheers!

  8. Jeff Monson on April 16, 2011 at 5:24 pm

    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!

    • Meggan on April 17, 2011 at 6:29 am

      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).

    • c.bavota on April 17, 2011 at 9:35 am

      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.

  9. Sally on April 18, 2011 at 6:50 pm

    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!!!

    • Meggan on April 19, 2011 at 12:37 am

      At last, I’m clarified. Thanks for the information! I will certainly use it.

  10. Fred on April 19, 2011 at 9:09 pm

    Thanks for the code. I need this to check all my sites for child pages.

  11. Daniel on April 25, 2011 at 3:32 pm

    Thank you so much. You have no idea how much time you saved me.

  12. Sam on April 29, 2011 at 11:12 am

    Thanks for this! It is awesome…I will be using it.

  13. Dave on May 23, 2011 at 11:04 am

    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?

  14. Jeremy Referencement on May 24, 2011 at 5:37 pm

    Simple and clean way of doing it.

    I’ve used Steve’s way of doing it which looks more simple.

    Good day man!