Created
May 8, 2013 19:45
-
-
Save mattkeys/5543094 to your computer and use it in GitHub Desktop.
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 | |
/* | |
* Are You My Mother? by: Matt Keys | |
* Function: Pass an array of Prospective Page IDs along with the current Page ID. | |
* Return true if the current page is a child of any of the Prospective Page IDs. | |
* Optional: Set third parameter to TRUE if you want to check if the current page is a direct match to a Prospective Page ID | |
*/ | |
function are_you_my_mother(array $prospective_page_ids, $current_page_id, $match_parent = false) { | |
// If match_parent is true, see if current page is a direct match before trying ancestors | |
if($match_parent && in_array($current_page_id, $prospective_page_ids)) { | |
return true; | |
} | |
// Get the ancestors of the $current_page_id | |
$child_ancestors = get_post_ancestors($current_page_id); | |
// Check each ancestor against our $prospective_page_ids. Return true if there is a match. | |
foreach($child_ancestors as $ancestors) | |
{ | |
if(in_array($ancestors, $prospective_page_ids)) { | |
return true; | |
} | |
} | |
} | |
// EXAMPLE USAGE: | |
if(are_you_my_mother(array('1605','1281','8526'), $post->ID, true)) { | |
//DO SOMETHING | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment