WordPress: is_child function

Every once in a while I would like to test whether the current page is a descendant of another page. This is particularly useful if you want to display certain content on a subset of your pages only. Although the Codex suggests a similar is_tree function I would like to present an extended version here.

I’ve packaged the functionality inside a WordPress plugin called is_child that you can download here. Unzip it, upload it to wp-content/plugins and activate the plugin.

Coding is_child

Doing a quick web search brings up tons of sites providing some sort of is_child or is_tree function. Most of the time it’s something like the following:

function is_child($parent) {
  global $post;
  return $post->post_parent == $parent;

This is great and a pretty nice solution. I’ve taken this approach and extended it with the following functionality:

  • by default we’ll retrieve all parent pages up to the root node and test whether the current page is part of this tree. This behavior can be disabled though.
  • you can supply the name (slug) of a post/page instead of the ID and the function will look it up for you.

All this combined with some error checking makes a great idea even better. Just have a look at the code if you would like to know more about the exact implementation.


We’ve seen that it’s very easy to test whether the current page is a child of another page or part of a certain tree in your page hierarchy. Packaging this into a plugin or placing the necessary code into your theme’s functions.php allows you to display certain content on particular pages only. In case you’re using the Widget logic plugin you now can display widgets on pages belonging to a particular subtree of your pages only, which is great.

10 thoughts on “WordPress: is_child function”

  1. How about a little help with this plug-in….
    Can you spell it out. Step 123…

    Do I need to add this…

    function is_child($parent) {
    global $post;
    return $post->post_parent == $parent;

    to my functions.php or not? I’ve seen it implemented around the web “Most of the time it’s something like the following:” not clear!

    can you give an example of how to call this function would it be added to the (list item) of the subpage or to the (list item) that holds the subpages? not clear

    Installing this plug-in didn’t seem to change my themes functions.php was it supost to? help!

  2. Hi Chris,
    this post was meant for programmers with a basic understanding of PHP and WordPress in general. I’m sorry that this post isn’t that clear for you but here’s how to use the plugin:

    1. install and activate the plugin. Note that this won’t change your theme’s functions.php in any way.
    2. use the function is_child where ever you want to make sure that the current post/page is a descendant of the given parent post/page.

    Say, you’ve got a page with the ID 4711 and another page with the ID 4712 where the latter is a child page of the first one. The function call is_child(4711) will evaluate to true if you’re requesting any of this page’s sub pages, e.g. the one with the ID 4712.

  3. Hi,

    Thanks for the help, I was looking all over for this and your is_child plugin saved me a lot of work. I’m pretty new to PHP and always looking for any help I can get. I was surprised how hard it was to tell WordPress to show content on the About and page and all the About page’s children. Why is it so hard? Why did you have to write so much code in is_child to make that work.

    Here is the function I added to my custom-functions.php:

    function about_menu() {
      if (( is_page('about')) || (is_child('about'))){
        wp_nav_menu( array( 'container_class' => 'about-menu',
                            'theme_location' => 'about' ) );

    This of course makes sense to me, but without your is_child in there, is there anyway else you could do it in WordPress? Seems silly that this is not built into their list of conditionals.

    Thanks again,

  4. Hi Jeff,
    basically the plugin was pretty easy to write, partly due to the straight forward database schema of WordPress which made it easy grabbing the needed information. In fact, the codex mentions similar ways solving this kind of issue. Since I wanted a solution that could not only handle post IDs but slugs as well and would work for more than one hierarchy – a is a parent of b, b is a parent of c and is_child('a') returns true on page c as well – I created this plugin. Short answer, WordPress has (limited) support for this – if you want a nice, clean solution simply use this plugin 😉

  5. Hi Christian,

    This looks just like what I need for my site, but it doesn’t seem to be working for me.

    I have the following code in my header.php:

    <?php if ( (is_page('Sabah, BorneoSabah, Borneo')) || (is_child('Sabah, BorneoSabah, Borneo')) ): ?>

    I know it’s not very pretty, but I’m new to all this. The problem is the menu displays on the parent page but not the children page.

    When I use ‘array’ within ‘is_page’ and list all the page names manually it works, but obviously that is not the best solution to my problem.

    Could you point me in the right direction to fix my problem please?


  6. Hi Jemma,
    you’ll have to supply the slug or the ID of the post in the is_child function. Have you tried the slug, i.e. something like sabah-borneosabah-borneo, or the post’s ID instead yet?

  7. Thank you, that’s fixed it, I was just using page names, instead of slugs, which works for is_page, but not is_child. What a useful plugin, thanks very much!

Comments are closed.