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
};
What does this code do exactly, it checks if your on a page and $pid is the id of a page or the current page’s post parent. Don’t get how it could be useful. Why not just use !empty($post->post_parent)?
I use it to load custom sidebars or special style rules for sub-pages of a parent… or you can use it like the Snippet#3 example, to load a custom header image for different pages without having to write arrays or complex if/elseif/else logic. the possibilities are numerous and as one who has used WordPress as a CMS for years, this function really eased my workload.
You can use it in an array of pages… so, rather than use if_page(‘x’) || is_page(‘x’) etc etc for all sub-pages of a parent page, you can simply declare the parent and apply your logic to its IMMEDIATE child pages without applying it to THEIR children.
That’s the main thing, for me. If you use something based on child_of=, then it applies to all of them
Call me crazy, but this has become a favorite for me.
just to add one more thing… the beauty of this for me is not having to know the ID or name or page-slug of any FUTURE sub pages of a parent page.
When I build the array of parent pages, I don’t have to know about the children.
It is fabulous, I use it on nearly every theme I build now. It eliminates the need to choose templates every time you create a page, and so much more. Thank you!!!
Very usefull. I use the Snippet 4 but it would better if we could use array() as is_page() and is_single(). Any ideas?
Great script. I’ve updated it to work with navigation with more than two levels deep without losing the absolute parent.
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;
}
};