is_subpage() WordPress function to check page parent

This custom conditional function for WordPress allows to check if the current page is a sub page. It accepts either a page slug post_name or an integer ID to check against. If it matches it will return a true value or otherwise false. Only for 3.1 and upwards.

* Is SubPage?
* Checks if the current page is a sub-page and returns true or false.
* @param  $page mixed optional ( post_name or ID ) to check against.
* @return boolean
function mv_is_subpage( $page = null )
    global $post;
    // is this even a page?
    if ( ! is_page() )
        return false;
    // does it have a parent?
    if ( ! isset( $post->post_parent ) OR $post->post_parent <= 0 )
        return false;
    // is there something to check against?
    if ( ! isset( $page ) ) {
        // yup this is a sub-page
        return true;
    } else {
		// if $page is an integer then its a simple check
		if ( is_int( $page ) ) {
            // check
            if ( $post->post_parent == $page )
                return true;
		} else if ( is_string( $page ) ) {
			// get ancestors
	        $parent = get_ancestors( $post->ID, 'page' );
	        // does it have ancestors?
	        if ( empty( $parent ) )
	            return false;
			// get the first ancestor
			$parent = get_post( $parent[0] );
            // compare the post_name
            if ( $parent->post_name == $page )
                return true;
        return false;
if ( mv_is_subpage('products') OR mv_is_subpage( 8 ) )
    //do something

Reactions (17)

  1. Pingback Custom WordPress function to check page parent | Webmaster Tools

  2. Pingback WordPress. Una funzione per controllare la pagina madre — Studio404 Web Agency

  3. Robert Neuschul

    Hi Matt

    I’m clearly doing something wrong, but I can’t figure this out.

    Have added the is_subpage() function to my template theme, functions.php and am using widget logic control in an attempt to build context sensitive subpage menus in the sidebar.

    Thus if I have two top level menu items called “home” and “about” I would want to show different sidebar menus in each. The conventional widget logic for the sidebar on the home page would then be “is_page(‘home’) || is_subpage()” and on the about page would be “is_page(‘about’) || is_subpage()” – and each instance would then show different subpages in different locations.

    What actually happens is that the top level menu items show the correct context sensitive menu, but any sub pages show ALL submenus – both those from “home” and “about”.

    I really don’t want to have to “name” each of the subpage instances explicitly in each usage since that renders it pointless having/using the subpage function in the first place.

    Not being a programmer this may be the wrong description, but it feels as if your function is creating only one global instance of the subpage array and not clearing that array when a user moves to a different page/subpage.

    Hope this description makes sense. Any help or pointers would be most useful.

    • Hi Robert, thanks for the comment. I understand your problem, what you need is to pass the ID also of that one top page you are checking. For example if you Home page ID is 3 and your About page ID is 5 it would be:

      “is_page(’home’) || is_subpage(4)”
      “is_page(’about’) || is_subpage(5)”

      Otherwise it wil show you the subpages sidebar each time you are on a subpage no matter which parent page they have. Hope that helps!

  4. Matt,

    Very helpful function. I added a reference to it from the WordPress Codex.

    Quick question – Could you write an update that would accommodate for the “slug name” as well as the ID? This is especially useful when building a theme on a dev server to be transferred live where IDs may change, but slugs stay the same.

    Thank you, sir!

  5. Pingback is_subpage() and is_descendant() - WordPress Tavern Forum

  6. There is a more efficient way, yo have only to check the post_parent variable:

    function is_subpage($the_id){
    global $post;
    if ($post->post_parent == '20') {
    return true;
    } else {
    return false;

  7. I just used:
    function is_subpage($the_id){
    global $post;
    if ($post->post_parent == $the_id) {
    return true;
    } else {
    return false;

    if(is_subpage(11)) {
    // do something

    works like a charm!

  8. Hi Matt and Dave(Above)

    Thanks for the tips and functions you helped me see the light at the end of the tunnel…. i’m very nearly there just one more problem.

    Im using this alongside WidgetLogic and it both functions behavely a bit unexpectedly.

    Dave: I can’t seem to use !is_subpage(48) to keep the generic menu from showing on children of 48, I also couldn’t invert the true and falses’ to get a is_not_subpage(48).

    Do you have any ideas how I could fix this…?

    Matt: Again thanks for your function that has really helped it me learning the power of WordPress aswell as give me home for my problem.

    What happens with yours is when I try and use it reversed alongside another reversed tag; for instance. I dont want to show the menu for children of 48 on the front page or any page other than those with a parent of 48, and so my WidgetLogic looks like this: !is_subpage(48) || !is_front_page

    For added info this logic in another widget works without fault: is_subpage(48) || is_page(48) || !is_front_page() It seems to arise when using the reverse of the tag…..

    Thanks again and heeeellllpp…..

  9. I should have added that when I use !is_subpage(48) || !is_front_page it hides it on sub pages of 48 but no longer on the front page…..


  10. Thank you so much. Crazy thi si not a part of the wordpress codex.

  11. Nice, I’d left a comment awhile ago about doing this with your previous version of is_subpage qnd whaddya know, here it is.

    Another question, if you don’t mind: should I be able to use !is_subpage() and !is_subpage(10)? Doesn’t seem to produce the desired affects…

  12. Found this after giving up on the is_subpage version in the Codex. Works well except for one instance that was messing me up with their version too: if the subpage has a different page template than the parent page, it won’t work.

    seems to be a number of reasons why it hasn’t entered in as a proper function in wordpress yet.

  13. Like David said, why is his not a par of the WordPress codex? As useful as this is, it’s a no brainer. In other words, even if you had no brain it would still make sense!

  14. I am just trying to learn wordpress from different online sources and it is good to get this post because this is one of the most common problem that new wordpress users face frequently. Thank you for this post.

    -mohsinmallik@Amish country bed and breakfast expert

  15. How can I check current page is home page or other.

    I try this syntax

    if ( is_home() ) { echo ‘ class=”current_page_item”‘; }

    But not working I use wordpress 3.3

    Any idea…???