Forums

a way to test if pages are children of a parent? (3 posts)

  1. syncbox
    Member
    Posted 1 year ago #

    Is there a way to test if a page is the child of a specific page so that you can then write specific code? For example, let's say I have 3 main menu links that link to 3 "parent" Pages. Each of these have many, many sub (child) pages of their own.

    In my header file (which houses this shared navigation code and markup), I want to say something like

    <a href="/category/upcoming-events/" title="upcoming events" onmouseover="P7_autoLayers(1)" <?php if (is_page(3) || is_page(26)) {echo 'id="selected"';} else {} ?>>upcoming events</a>

    But that is the easy one, as it only applies to 2 places in the site. The others have many children... so I want to try a more dynamic approach, rather than specifically stating the page ids... replacing this part

    (is_page(3) || is_page(26))

    with something that summarizes as "if child of page 4" {do this} else {dont do anything}

    This would be for 3 other main menu links.

    I hope I've explained this well. I know how to use child_of= in page lists, etc but have never found a clear answer of how to "test" or write conditional code that would return whether the current page was a child of a specific page id.

    help SO greatly appreciated! Thanks for input in advance

  2. syncbox
    Member
    Posted 1 year ago #

    sorry, somehow this post thinks it should be under installation and I cannot figure out how to change that. Can someone move this to a more appropriate place? This question has nothing to do with installation

  3. MichaelH
    moderator
    Posted 1 year ago #

    The variable $post->post_parent would contain the post id of the parent page.

    See this thread for a lesson from Kaf:
    http://wordpress.org/support/topic/144403?replies=24

Topic Closed

This topic has been closed to new replies.

About this Topic