Support » Fixing WordPress » How to determine if current page is a child of another

  • Resolved davidjg


    I have a navigation bar made up of links to six pages. I have more than six pages overall, and each of the six in the navigation has child pages. The links to the pages are hand coded – I’m not using wp_list_pages.

    I am trying to give the appropriate link in the navigation class="current" when on either that page or a child of that page (so if on page ‘foo’ or child of ‘foo’, the link to ‘foo’ in the nav has class="current"). Due to the host this is on refusing to install ISAPI_rewrite (IIS equivalent of mod_rewrite), I can’t do this by comparing the URL to anything (if you have a page ‘bar’ as a child of ‘foo’, the pretty URL would be /foo/bar, so you could say “if URL contains ‘foo’, add class="current"“. I’m stuck with “/?page_id=X”).

    So – is there a way of determining if the current page is a child of another page? I wish to be able to say “if current page is page X or a child of page X do this, else do that”.

    Thank you for any help you can offer!

Viewing 4 replies - 1 through 4 (of 4 total)
  • What you want done cannot be done without custom code .. at least to the extent I am aware of ..

    You can check for what page it is given you know the ID or the page or the page-name ..

    So potentially if you have a page “bar” as a child of the page “foo” .. you could check the following condition:

    if (is_page('bar'))
    // Do whatever you want like highlighting page 'foo' link

    But you would have to do this manually for every child-parent page combination ..

    Another possible way is to generalize your PHP code, and check the database directly to see if the current page has a parent .. if it does, get the ID of the parent page, and then do whatever you want to it ..

    Hope this helps. If something is not clear, post back and I will try explain more ..

    Can is_page() be used like this:

    if (is_page(12||13||14))

    Or will I have to use

    if ((is_page(12))||(is_page(13))||(is_page(14)))

    I wish there was a is_child_of(X)

    Could you explain what you mean by “Another possible way is to generalize your PHP code”?

    (For anyone else with a slightly less complex dynamic menu highlighting issue, these links might come in handy: Codex Entry. WP Pages Nav.)

    That is a good suggestion with the is_child_of() .. maybe soon it will be part of the core ..

    Well technically if you proficient in PHP you could just write up the is_child_of() function yourself, and add it to one of the wp-includes function files .. Just remember when you upgrade you will have to keep this in mind etc ..

    And as for the generalized way, its pretty much what a is_child_of() function would do .. but now you would just directly add it to the template files rather ..

    A rough outline for this method would be:

    1. Get the current page id
    2. Check with the database to see if the field called post_parent is set to anything other than 0.
    3. If it is, then get the ID of that page (you might have to do this recursively to get to the meta parent depending on how the pages heirarchy is)
    4. Once you have the final parent page ID, do whatever highlighting etc for the link

    Obviously implementation isn’t exactly as simple as listing it in 4 items .. 🙂 .. but you get the idea now hopefully


    And yes you will have to use the following structure

    if ((is_page(12))||(is_page(13))||(is_page(14)))

    Ok – depending on time, I might have a go at creating a function (heh…might be more effort than it’s worth, given my skills), but I’ll probably just go with if((is_page(X))||(is_page(Y)))

    Thanks for your help skulled.

    EDIT – Just found this Support topic, which may help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to determine if current page is a child of another’ is closed to new replies.