Skip to content

Instantly share code, notes, and snippets.

@vanbernaert
Last active August 29, 2015 14:20
Show Gist options
  • Save vanbernaert/284982c517e54b4a9ef1 to your computer and use it in GitHub Desktop.
Save vanbernaert/284982c517e54b4a9ef1 to your computer and use it in GitHub Desktop.
Wordpress: do only if page has ancestor
<?php
function has_ancestor($pid)
{
global $post;
$ancestors = get_post_ancestors($post->$pid);
$root = count($ancestors);
if ( $root < 1) { return false; } // set to false in case of no ancestors
$parent = $ancestors[$root];
if(is_page() && (is_page($pid) || $post->post_parent == $pid || in_array($pid, $ancestors)))
{
return true;
}
else
{
return false;
}
};
$id = get_the_ID();
if (has_ancestor($id )) {
// do stuff when true, eg. showing a breadcrumb
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment