Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
For creating conditional based logic for page or any children pages that are under a parent page.
function if_page_or_ancestor_page_is($page_slug = NULL)
{
$is_page = false;
if (is_page()) {
global $post;
/* Get an array of Ancestors and Parents if they exist */
$parents = get_post_ancestors($post->ID);
$all_pages = array_reverse(array_merge($parents, array($post->ID)));
foreach ($all_pages as $page_id) {
$page = get_page($page_id);
if ($page->post_name == $page_slug) {
$is_page = true;
}
}
}
return $is_page;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment