WordPress.org

Ready to get started?Download WordPress

Forums

Fatal Error while trying to get parent page permalink (3 posts)

  1. albern
    Member
    Posted 3 years ago #

    Hi, I has been trying to display a parent page permalink on child page in two ways and I'm unsuccessful in both:
    1) <?php
    $parent_link = get_permalink($page->page_parent);
    echo $parent_link;
    ?>

    in this way it displays the page itself link but not the parent page;

    2) ?<php
    $parent = get_page($page->page_parent);
    echo $parent;
    ?>
    in this way I get:
    Catchable fatal error: Object of class stdClass could not be converted to string.

    I can't understand why I do not get parent page (not post) link. Help pls.

  2. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Using method #2, get_page( $page->page_parent ); returns an object.

    So, to get the ID out of $parent, you will need to use:

    $parent->ID

    Try something like:

    $parent = get_page( $page->page_parent );
    $parent_id = $parent->ID;
    $parent_link = get_permalink( $parent_id );
    echo $parent_link;
  3. albern
    Member
    Posted 3 years ago #

    Thank you for replying to my post.
    I tried this and I get the page itself link, not the parent page link.

Topic Closed

This topic has been closed to new replies.

About this Topic