Found these snippets on CSS Tricks while searching for a way to target both the parent and child page in order to pull data from the parent to output to all children.
They weren’t quite what I needed but I’m sure will come in handy down the road so wanted to store for safekeeping. If anyone can use them as well, I always consider that a win. Happy Coding!
<?php | |
// Check for a specific page | |
if ( is_page(2) ) { | |
// stuff | |
} | |
// Check if page is child of a certain page | |
if ( $post->post_parent == '2' ) { | |
// stuff | |
} | |
// Is page a parent or child of the parent | |
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath | |
global $post; // load details about this page | |
if(is_page()&&($post->post_parent==$pid||is_page($pid))) | |
return true; // we're at the page or at a sub page | |
else | |
return false; // we're elsewhere | |
}; | |
// Is page a parent, child or any ancestor of the page | |
function is_tree($pid) | |
{ | |
global $post; | |
$ancestors = get_post_ancestors($post->$pid); | |
$root = count($ancestors) - 1; | |
$parent = $ancestors[$root]; | |
if(is_page() && (is_page($pid) || $post->post_parent == $pid || in_array($pid, $ancestors))) | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
}; | |
// Usage | |
if (is_tree(2)) { | |
// stuff | |
} |