Last active
December 19, 2015 09:48
-
-
Save mircobabini/5935274 to your computer and use it in GitHub Desktop.
Work-with-parents functions for 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 get_topmost_parent_id ($post_id) { | |
| if ($post_id === null) | |
| $post_id = get_the_ID(); | |
| $parent_id = get_parent_id ($post_id); | |
| if ($parent_id == 0){ | |
| return $post_id; | |
| } else { | |
| return get_topmost_parent_id ($parent_id); | |
| } | |
| } | |
| function get_parents_ids ($post_id = null) { | |
| if ($post_id === null) | |
| $post_id = get_the_ID(); | |
| $parents_ids = array (); | |
| $parent_id = get_parent_id ($post_id); | |
| while ($parent_id != 0) { | |
| $parents_ids[] = $parent_id; | |
| $parent_id = get_parent_id ($parent_id); | |
| } | |
| return $parents_ids; | |
| } | |
| function get_parent_id ($post_id) { | |
| if ($post_id === null) | |
| $post_id = get_the_ID(); | |
| return get_post($post_id)->post_parent; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment