Created
August 6, 2015 18:11
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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