Skip to content

Instantly share code, notes, and snippets.

@fengelz
Created March 21, 2012 09:31
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 fengelz/2145808 to your computer and use it in GitHub Desktop.
Save fengelz/2145808 to your computer and use it in GitHub Desktop.
Get page depth in wordpress
function get_page_depth($post) {
$parent_id = $post->post_parent;
$depth = 0;
while ($parent_id > 0) {
$page = get_page($parent_id);
$parent_id = $page->post_parent;
$depth++;
}
return $depth;
}
@mmaarten
Copy link

mmaarten commented Nov 21, 2017

what about:

function get_page_depth( $post )
{
	$ancestors = get_ancestors( $post->ID, 'page' );

	return count( $ancestors );
}

Regards,

Maarten

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment