Skip to content

Instantly share code, notes, and snippets.

@ajithrn
Last active August 29, 2015 14:16
Show Gist options
  • Save ajithrn/a8c82616e81c305b633c to your computer and use it in GitHub Desktop.
Save ajithrn/a8c82616e81c305b633c to your computer and use it in GitHub Desktop.
WordPress: Check if a page is child of another
/**
* Check if a page is child of another
* Ref: http://bavotasan.com/2011/is_child-conditional-function-for-wordpress/
*
* @param mixed $page_id_or_slug Provide the parent ID or Slug
* @return bool Whether the page is child or not of the given parent
*/
function is_child( $page_id_or_slug )
{
global $post;
if( !is_page() )
return false;
# Slug provided, get ID
if( !is_int( $page_id_or_slug ) )
{
$page = get_page_by_path( $page_id_or_slug );
$page_id_or_slug = $page->ID;
}
return $post->post_parent == $page_id_or_slug;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment