Skip to content

Instantly share code, notes, and snippets.

@Vyygir
Created February 20, 2017 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vyygir/207548c40e108d969bf59175b228a209 to your computer and use it in GitHub Desktop.
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
<?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