function get_top_parent_page_id() {

global $post;

// Check if page is a child page (any level)
if ($post->ancestors) {

// Grab the ID of top-level page from the tree
return end($post->ancestors);

} else {

// Page is the top level, so use it's own id
return $post->ID;

}

}

$parent_page_id = get_top_parent_page_id($post->ID);

http://www.webcitizenmag.com/2010/05/20/how-to-get-top-parent-page-id-in-wordpress/

Advertisements