Created
January 17, 2019 14:35
-
-
Save jonbrockett/288b070962f4b413ebc1c601c38dced1 to your computer and use it in GitHub Desktop.
Get Top Parent Page (Custom WP Function)
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
| /** Current Page's Parent */ | |
| require_once( 'library/get_top_parent.php' ); |
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 | |
| /** | |
| * Get the Top Parent | |
| * 04/26/2018 | |
| * @author Jonathan Brockett | |
| * | |
| * Requires: WordPress @link https://codex.wordpress.org/ | |
| */ | |
| /** | |
| * Gets the top parent page ID | |
| * @return string The ID of the top parent page of any child page | |
| */ | |
| function get_the_top_parent() { | |
| $current_page_id = get_the_ID(); | |
| $current_page_ancestors = get_post_ancestors( $current_page_id->ID ); | |
| $current_page_top_parent = ($current_page_ancestors) ? $current_page_ancestors[count($current_page_ancestors)-1]: $current_page_id; | |
| return $current_page_top_parent; | |
| } | |
| /** | |
| * Displays the top parent page ID | |
| */ | |
| function the_top_parent() { | |
| echo get_the_top_parent(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment