WordPress: Is_Child Function

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

14 Responses to “WordPress: Is_Child Function”

  1. mIKE Says:

    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.

  2. Nicholas Roussos Says:

    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.

  3. mIKE Says:

    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!

  4. Nicholas Roussos Says:

    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 …

  5. Jens Törnell Says:

    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

  6. Will Says:

    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?

  7. Nicholas Roussos Says:

    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.

  8. Chris M Says:

    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?

  9. Nicholas Roussos Says:

    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.

  10. Wordpress is_child() for Advanced Navigation | kevinleary.net Says:

    [...] the conditional in your theme(s), add the code above to functions.php. Special thanks goes out to Nicholas Roussos for creating this wonderful [...]

  11. get_depth() - get depth of a page or category - Wordpress template tag / function | devdevote.com Says:

    [...] Nicholas Roussos – is_child function [...]

  12. Buy microsoft office 2010 product Key Says:

    Buy microsoft office 2010 product Key…

    [...]Wordpress: Is_Child Function / Nicholas Roussos[...]…

  13. bedroom Says:

    holidays…

    [...]Wordpress: Is_Child Function / Nicholas Roussos[...]…

  14. Phuong Natvig Says:

    This is where I would get some help/info at http://thecarhunters.com/!

Leave a Reply