Skip to content

Instantly share code, notes, and snippets.

@philcook
Created September 1, 2016 13:54
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save philcook/15bd50aeb8f80315d2bcaa9720c45bbb to your computer and use it in GitHub Desktop.
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