Created
February 20, 2017 13:56
-
-
Save Vyygir/207548c40e108d969bf59175b228a209 to your computer and use it in GitHub Desktop.
Quick, small function to check if your page is currently the nth child
This file contains 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 | |
/** | |
* is_nth_level_page | |
* | |
* Quick, small function to check if your page is currently the nth child | |
* | |
* @param int $n The page level you want to check for | |
* @param int $page_id (optional) The ID of the page to check | |
* | |
* @return boolean | |
*/ | |
function is_nth_level_page($n, $page_id = null) { | |
global $post; | |
if (!isset($post) && $page_id == null) { | |
return false; | |
} else if ($page_id == null) { | |
$page_id = $post->ID; | |
} | |
$ancestors = get_ancestors($page_id, 'page'); | |
return !empty($ancestors) ? (count($ancestors) == $n - 1) : false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment