Last active
August 29, 2015 13:56
-
-
Save joshuadavidnelson/9331774 to your computer and use it in GitHub Desktop.
Determine if post type is a child of another or is the parent.
This file contains 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 | |
/** | |
* Check is post is the child, sibling or the parent. | |
* | |
* Modified from http://css-tricks.com/snippets/wordpress/if-page-is-parent-or-child/ | |
**/ | |
function is_branch( $pid ) { // Check if parent or child | |
global $post; | |
if( is_page() && is_int( $pid ) && ( $post->post_parent == $pid || is_page( $pid ) ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function is_child ( $pid ) { // Check if child only | |
global $post; | |
if( is_page() && is_int( $pid ) && ( $post->post_parent == $pid ) ) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
function is_sibling( $pid ) { // Check if page is a sibling of another | |
global $post; | |
if( is_page() && is_int( $pid ) && ( $post->post_parent != $pid ) ) { | |
$my_ancestors = get_post_ancestors( $post ); | |
$pid_ancestors = get_post_ancestors( $pid ); | |
if( !empty( $my_ancestors ) && !empty( $pid_ancestors ) ) { | |
if( in_array( $my_ancestors, $pid_ancestors ) ) | |
return true; | |
} | |
} else { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment