Skip to content

Instantly share code, notes, and snippets.

@joshuadavidnelson
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuadavidnelson/9331774 to your computer and use it in GitHub Desktop.
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.
<?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