Skip to content

Instantly share code, notes, and snippets.

@rabbitinblack
Created November 17, 2024 12:03
Show Gist options
  • Select an option

  • Save rabbitinblack/ecb9aff68a8fd61d8fc1680c3d94f9d3 to your computer and use it in GitHub Desktop.

Select an option

Save rabbitinblack/ecb9aff68a8fd61d8fc1680c3d94f9d3 to your computer and use it in GitHub Desktop.
Function get the top-level parent of page in WordPress
<?php
/**
* Function to get the top-level parent of a page.
*
* @param int $post_id The ID of the page.
* @return int The ID of the top-level parent page.
*/
function get_top_level_parent($post_id) {
$parent_id = wp_get_post_parent_id($post_id);
if ($parent_id == 0) {
return $post_id;
} else {
return get_top_level_parent($parent_id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment