Skip to content

Instantly share code, notes, and snippets.

@firebelly
Created May 19, 2011 16:32
Show Gist options
  • Save firebelly/981184 to your computer and use it in GitHub Desktop.
Save firebelly/981184 to your computer and use it in GitHub Desktop.
get the topmost parent of a wordpress page
<?php
// recursion is our friend here
function get_topmost_parent($post_id){
$parent_id = get_post($post_id)->post_parent;
if($parent_id == 0){
return $post_id;
}else{
return get_topmost_parent($parent_id);
}
}
$parent = get_post(get_topmost_parent($post->id));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment