Skip to content

Instantly share code, notes, and snippets.

@jimmynotjim
Forked from ericrasch/is_child.php
Created February 6, 2013 15:52
Show Gist options
  • Select an option

  • Save jimmynotjim/4723465 to your computer and use it in GitHub Desktop.

Select an option

Save jimmynotjim/4723465 to your computer and use it in GitHub Desktop.
<?php
/* =BEGIN: Check If Page Is Parent/Child/Ancestor
Source: http://www.stemlegal.com/greenhouse/2012/checking-ancestor-and-parent-pages-in-wordpress/
---------------------------------------------------------------------------------------------------- */
function is_tree( $page_id_or_slug ) {
global $post;
if ( !is_numeric( $page_id_or_slug ) ) { // Used this code to change a slug to an ID, but had to change is_int to is_numeric for it to work: http://bavotasan.com/2011/is_child-conditional-function-for-wordpress/
$page = get_page_by_path( $page_id_or_slug );
$page_id_or_slug = $page->ID;
}
if ( is_page( $page_id_or_slug ) )
return true;
$anc = get_post_ancestors( $post->ID );
foreach ( $anc as $ancestor ) {
if ( is_page() && $ancestor == $page_id_or_slug ) {
return true;
}
}
return false;
}
?>
<?php if ( is_tree( 'subpage' ) ) { // Using the slug /subpage/, but you could also use the ID of /subpage/ ?>
[[... insert your code here ...]]
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment