Last active
August 29, 2015 14:20
-
-
Save vanbernaert/284982c517e54b4a9ef1 to your computer and use it in GitHub Desktop.
Wordpress: do only if page has ancestor
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 | |
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