Code Snippet

Home » Code Snippets » WordPress » If Page Is Parent or Child

If Page Is Parent or Child

There are built in conditional WordPress functions for testing for a page:

if ( is_page(2) ) {
  // stuff
}

Or for testing if a page is a child of a certain page:

if ( $post->post_parent == '2' ) {
  // stuff
}

But there is no built in function that combines these two things, which is a fairly common need. For example, loading a special CSS page for a whole "branch" of content. Like a "videos" page and all its children individual videos pages.

This function (add to functions.php file) creates new logical function to be used in this way:

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
};

Usage

if (is_tree(2)) {
   // stuff
}

Subscribe to The Thread

  1. Paul

    Hey,

    how can I make it work with categories?

    if I just replace ‘is_page’ with ‘is_category’ it is true for the category overview but not for alle the articles in it..

    Paul

  2. This is good, but it only works one level deep.

    From the WordPress Codex:
    “Note that if you have more than one level of pages the parent page is the one directly above and not the one at the very top of the hierarchy.”

    See more about this at the WP Codex: Conditional Tags

    P.S.
    I hope noone made a buck out of this snippet, it’s straight from the wordpress codex.

    • Port 80

      NB I had this problem ‘only works one level deep’

      But this function works with all ‘ancestors’ not just single level ‘child’ pages

      ***also take straight from the WP codex***
      http://codex.wordpress.org/Conditional_Tags

      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
      };

  3. Nice snippet here!

  4. Brenna

    How can you use this if you don’t want to specify a certain page ID. I need to be able to check dynamically if any current page I happen to be on is part of a branch (whether it’s the parent or child), and if so, then output a menu depending on whatever page ID it automatically retrieves.

    Any help or suggestions would be appreciated.

  5. Thanks for this! :)

  6. Thank you! Needed this for my WordPress theme sidebar :)

  7. Cool, thanks so much for posting this! Very useful =)

  8. orangerie

    Just great, thanks !

  9. Great tutorial! Thanks! I also have another approach on this.

    
    <?php  global $post;
    if ( is_page('2') || $post->post_parent == '2'){
    	//stuff if page has id of 2 and its subpages
    }
    else {
    	//stuff if not
    }
    ?>
    
    • i figure it out that the above tutorial is better than my approach. I’m going to use that approach from now on.

  10. Had to tackle this today and the below worked perfectly for me (works with any nesting level)

    
    if (is_page(27) || in_array(27, $post->ancestors))
    {
       // stuff if your page has ID of 27 or is a subpage (any nested level) of page ID 27
    }
    
    
  11. Hi there,
    I’m just wondering about this coding. I have a website that I’m building where the Parent has Child pages. Eg… I have a Parent page that is called:

    ABOUT US (parent page)
    Company Overview (child page)
    Director (child page)

    The company Overview page is fine because its the same page as about page. But when I click on Director Page I want the Parent Page “ABOUT US” to be highlighted as well. So both pages would be highlighted the child and parent page. The coding I’ve done is really simple. Its as follows

    ABOUT US – nav coding.

    <a class="about_us" href="”

    >

    and DIRECTOR – nav coding.

    <a href="”
    class=”director”>

    Is there a coding where I can say if the parent page we are on is “ABOUT US” to make it show it selected as well? Can you write me an example please

    Thanks in Advance.
    Oh and the link for the website is http://www.fairwayviewapartmentssamoa.com

  12. Thank you tremendously for sharing this. I have come across your site multiple times now while searching for help on various things. Keep it up, and tank you again.

  13. Anyway of making this work by slug?

    I imagine we’d have to get the id from the slug.

    I’m gonna play around some this weekend.

  14. This was exactly what I needed!!!

    I was trying to edit the submenu navigational links on pages based on the child of a parent.

    In a nut shell, when on a child page, I needed the nav menu to reflect the links of the parent of the child not the child itself.

    This tutorial helped to push me in the right direction. Now I can add a custom class to the current page!

Speak, my friend

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
~ The Management ~