/**
* Gets the top most ancestor for a post.
*
* @param object $post The current post
*
* @return integer The post ID of the ancestor
*/
function get_top_ancestor( $post ) {
if ( $post->post_parent ) {
$ancestors = get_post_ancestors( $post->ID );
$root = count( $ancestors ) - 1;
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
}
return $parent;
}