is_tree()
-
I added this function to my functions.php:
<?php function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath global $post; // load details about this page $anc = get_post_ancestors( $post->ID ); foreach($anc as $ancestor) { if(is_page() && $ancestor == $pid) { return true; } } if(is_page()&&(is_page($pid))) return true; // we're at the page or at a sub page else return false; // we're elsewhere }; ?>
and I am executing it outside of the loop:
<?php if (is_tree('home')){ echo '<img src = "' . $siteUrl . '/images/content_title_productivityOptimisation.jpg" alt = "Welcome Title" />'; } elseif (is_tree('partners')){ echo '<img src = "' . $siteUrl . '/images/content_title_partner.jpg" alt = "Welcome Title" />'; } elseif (is_tree('blog')){ echo '<img src = "' . $siteUrl . '/images/content_title_blog.jpg" alt = "Welcome Title" />'; } elseif (is_tree('contact')){ echo '<img src = "' . $siteUrl . '/images/content_title_contact.jpg" alt = "Welcome Title" />'; } elseif (is_tree('productivity-tools')){ echo '<img src = "' . $siteUrl . '/images/content_title_productivityTools.jpg" alt = "Welcome Title" />'; } elseif (is_tree('training-workshops')){ echo '<img src = "' . $siteUrl . '/images/content_title_trainingWorkshops.jpg" alt = "Welcome Title" />'; } elseif (is_tree('productivity-platforms')){ echo '<img src = "' . $siteUrl . '/images/content_title_productivityPlatforms.jpg" alt = "Welcome Title" />'; }else{ } ?>
It is only working if is_page is true, rather than if it is a child of the parent identified in my if statements.
- The topic ‘is_tree()’ is closed to new replies.