Created
November 17, 2024 12:03
-
-
Save rabbitinblack/ecb9aff68a8fd61d8fc1680c3d94f9d3 to your computer and use it in GitHub Desktop.
Function get the top-level parent of page in WordPress
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 | |
| /** | |
| * 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