Last active
May 21, 2016 02:22
-
-
Save kjbrum/56f73a0e710194d8f758 to your computer and use it in GitHub Desktop.
Check if we are on a child or grandchild page of the page.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Check if we are on a child or grandchild page of the page. | |
* | |
* @param integer $id The ID of the page we're looking for pages underneath | |
* @param boolean $parent Whether to check if we are on the parent page as well | |
* @return boolean | |
*/ | |
function wp_is_child( $id=0, $parent=false ) { | |
global $post; | |
if( is_page() ) { | |
if( $parent && is_page( $id ) ) { | |
return true; // On the parent page | |
} else { | |
$parents = get_post_ancestors( $post->ID ); | |
foreach( $parents as $page_id ){ | |
if( $page_id == $id ){ | |
return true; // At a sub page of $id | |
} | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment