Last Updated: February 25, 2016
·
3.59K
· dreszczyk

[Wordpress] How to check if page is a child?

Paste this into your functions.php:

function is_child($pageid) { 
    global $post; 
    if( is_page() && ($post->post_parent == $pageid) ) {
       return true; // is child
    } else { 
       return false; // not child
    }
}

In your template use if like this:

if (is_child(X)) {
    //do somthing
}

1 Response
Add your response

Nice function, just for keeping it explicit would ischildof be a more descriptive name?

over 1 year ago ·

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

WordPress sites collect scripts from themes, plugins, and tag managers. A compromised plugin update can add a skimmer to every page without a change in your own templates.

Security insights from Cside