Support » Fixing WordPress » Get Parent Page link from Child Page

  • I want to get a link to the parent page from a child page. $post->post_parent returns the correct page ID, but this isn’t working:

    <?php wp_list_pages('title_li=&include='.$post->post_parent); ?>

    … why not?

    What am I doing wrong?

    Thanks guys 🙂

Viewing 3 replies - 1 through 3 (of 3 total)
  • Got to be in The Loop.

    <?php if ($post->post_parent != 0) {
    wp_list_pages('title_li=&include='.$post->post_parent);
    }
    ?>

    Thanks for your reply MichaelH, that’s exactly the line I had, only wrapped in a conditional…

    I just tried to put it in The Loop, and it still didn’t work.

    There’s no restriction on wp_list_pages being in the loop, of course, since we know it usually lives in the sidebar, and $post->post_parent works even WITHOUT a loop, actually returning the correct ID number (I can echo it to make sure).

    For some reason the wp_list_pages simply returns null when attempting to list the parent even if I manually type the ID number in the function args.

    Something screwy is going on.

    Cheers for the reply.

    I got this to work without an issue. I am on WP 2.2. I wanted to have a menu with a submenu and when you are on the submenu page i wanted it to continue to show the submenu items … ie the current page and its peers … I also highlight the menu item and the submenu item that you are on.

    It’s got a limitation in that every page has to be on the menu or submenu for it to work. I am sure though that there is an easy enough bit of logic that could be added to address this.

    Here is what i have in my header.php

    <div id="header">
      <ul id="nav">
        <?php
        $mainID = 3; /* set this to the parent of your menu items */
        wp_list_pages('child_of='.$mainID.'&sort_column=menu_order&depth=1&title_li=');
        ?>
      </ul>
    
      <div class="hr"><hr /></div>
    
      <ul id="nav">
        <?php
        /* get the parent ID */
        if ($post->post_parent != 0)
        {
          $parentID = $post->post_parent;
        }
    
        if ($parentID != $mainID)
        {
          /* we are not on a main menu page grab this page's siblings */
          /* this does assume that all pages are menu or submenu items */
          $submenuparentID = $parentID;
        }
        else
        {
          /* we are on a main menu page so grab this page's children */
          $submenuparentID = $post->ID;
        }
    
        wp_list_pages('child_of='.$submenuparentID.'&sort_column=menu_order&depth=1&title_li=');
        ?>
      </ul>
    </div> <!-- #header -->
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Get Parent Page link from Child Page’ is closed to new replies.