A function like is_page() but returns true if on any sub page of given page

In my header I use this code:

if(is_page("stores")) {
    // Do something
}

I run some code if on the page stores but what I want to do is run that code if the page is stores or any page that is a sub page of stores. Not only that, but is recursive so if we are on a sub sub sub page of stores the code still runs.

3 Answers
3

When using pretty permalinks, I simply check against the request;

if ( preg_match( '#^stores(/.+)?$#', $wp->request ) ) {
    // away we go!
} 

No extra DB queries, little computation, and works right down the tree!

Leave a Comment