Skip to content

Instantly share code, notes, and snippets.

@jonbrockett
Created January 17, 2019 14:35
Show Gist options
  • Select an option

  • Save jonbrockett/288b070962f4b413ebc1c601c38dced1 to your computer and use it in GitHub Desktop.

Select an option

Save jonbrockett/288b070962f4b413ebc1c601c38dced1 to your computer and use it in GitHub Desktop.
Get Top Parent Page (Custom WP Function)
/** Current Page's Parent */
require_once( 'library/get_top_parent.php' );
<?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