WordPress.org

Ready to get started?Download WordPress

Forums

get the parent id of a page (3 posts)

  1. gedtech
    Member
    Posted 6 years ago #

    Hi, I am trying to write a navigation scheme for a page that displays a list of not the parent, and nor the children of the current page, but just it's brothers and sisters. So, if my structure is

    1 - Cars
    * Ford
    * Honda
    * Chevy
    2 - Planes
    * Boing
    * Airbus

    and I was on the Honda page, I would want my list to display just Ford, Honda, and Chevy. I think the best way to do this is something like:
    [code]wp_list_pages('child_of=' . $pageParent);[/code]

    but I have googled and searched the codex and am unable to figure out how to get from WordPress the ID number of the parent of the page.

    The closest I found was this:
    [code]
    $object = $wp_query->get_queried_object();
    $parent_id = $object->post_parent;
    [/code]

    But it does not work, always returning a $parent_id of 0, even though that code is in the loop. I think I am misunderstanding the way WPQuery works.

    Any help would be appreciated. Thanks!
    -

  2. Clicknathan
    Member
    Posted 6 years ago #

    Hey Gedtech, try this out:

    <?php
    
    if($post->post_parent) { // page is a child
    
    wp_list_pages('sort_column=menu_order&title_li= &child_of='.$post->post_parent);
    
    }
    
    elseif(wp_list_pages("child_of=".$post->ID."&echo=0")) { // page has children
    
    wp_list_pages('sort_column=menu_order&title_li= &child_of='.$post->ID);
    }
    ?>
  3. evenglen
    Member
    Posted 6 years ago #

    Oh nifty Just what I was looking for.

Topic Closed

This topic has been closed to new replies.

About this Topic