Skip to content

Instantly share code, notes, and snippets.

@alenabdula
Created August 6, 2015 18:11
Show Gist options
  • Save alenabdula/1208411dc6f78fbc796f to your computer and use it in GitHub Desktop.
Save alenabdula/1208411dc6f78fbc796f to your computer and use it in GitHub Desktop.
Gets the id of the topmost ancestor of the current page and returns the current page's id if there is no parent.
<?php
/**
* --------------------------------------------------------
* Gets the id of the topmost ancestor of the current page.
* Returns the current page's id if there is no parent.
* @uses Object $post
* @return Int
* --------------------------------------------------------
*/
function get_post_top_ancestor_id () {
global $post;
if ( $post->post_parent ) {
$ancestors = array_reverse( get_post_ancestors( $post->ID ) );
return $ancestors[0];
}
return $post->ID;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment