Created
August 29, 2012 20:47
-
-
Save jtallant/3518723 to your computer and use it in GitHub Desktop.
Finding post depth 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
| function find_post_depth() { | |
| global $post; | |
| $parent = 0; | |
| $level = 1; | |
| if ($post->post_parent) { | |
| $parent = get_post($post->post_parent); | |
| $parents_parent = get_post($parent->post_parent); | |
| $parent_of_parents_parent = get_post($parents_parent->post_parent); | |
| } else { | |
| return $level; | |
| } | |
| // if the parent post has no parent we are at level two | |
| if ($parent->post_parent == false) { | |
| $level = 2; | |
| // if the parent of the parent post has no parent we are level three | |
| } elseif ($parents_parent->post_parent == false) { | |
| $level = 3; | |
| // if the parent of the parent posts parent has no parent we are at level four | |
| } elseif ($parent_of_parents_parent->post_parent == false) { | |
| $level = 4; | |
| } else { | |
| $level = 0; // anything over 4 levels | |
| } | |
| return $level; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment