WordPress.org

Ready to get started?Download WordPress

Forums

Get Parent Page link from Child Page (4 posts)

  1. Ivovic
    Member
    Posted 6 years ago #

    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 :)

  2. MichaelH
    Member
    Posted 6 years ago #

    Got to be in The Loop.

    <?php if ($post->post_parent != 0) {
    wp_list_pages('title_li=&include='.$post->post_parent);
    }
    ?>
  3. Ivovic
    Member
    Posted 6 years ago #

    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.

  4. tomkinsg
    Member
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.