Search code examples
wordpressif-statementchildrenconditional-statements

Wordpress if Child & Grandchild of Page


I'm serving a different header in Wordpress depending what section you are within the site, "residential" , "business" or other by using the following code:

<?php
    /*
    Template Name: Package
    */ 
    if (is_page('business') || $post->post_parent == "17")
    {
        get_header('business');
    }
    else if (is_page('residential') || $post->post_parent == "19")
    {
        get_header('residential');
    }
    else 
    {
        get_header();
    }
?>

Although I need to extend it to work with grandchildren as well, so another level down. Does anyone have an idea?


Solution

  • You could check to see if the page has an ancestor with the id you specified as the top level parent, i.e.

    if(in_array('19', get_ancestors($post->ID, 'page')){...}