WordPress.org

Ready to get started?Download WordPress

Forums

link to the parent of the current post. (6 posts)

  1. gilliancs
    Member
    Posted 6 years ago #

    I'm trying to put a link to the parent of the current page in the sidebar and I can't get the syntax right. I have taken the code form the get_post reference in the codex but I don't know what ' ->' means and I haven't been able to find a reference to it on the web.

    The code I'm using is:

    `<?php
    $parent_id=($post->post_parent);
    echo $parent_id;

    $parent_post = get_post($parent_id); 
    $parent_title = ($parent_id->post_title);
    echo $parent_title;
    ?> `

    I get: Parse error: syntax error, unexpected T_VARIABLE on line 23
    which is
    $parent_title = ($parent_id->post_title);

  2. Cathy Tibbles
    Member
    Posted 6 years ago #

    I'm confused by a post-parent. Are these posts in a series? Posts in a category? Or do you mean sub-cats? More info please.

  3. gilliancs
    Member
    Posted 6 years ago #

    Post_parent is the parent of the current post or page. (I'm using pages) (ie one up in the page hierarchy as set when you write a page.)

    The first part of the code works:

    $parent_id=($post->post_parent);
    echo $parent_id;

    so the line
    $parent_id=($post->post_parent);
    returns the id number of the parent of the current post.

    I've taken the code from here:
    http://codex.wordpress.org/Function_Reference/get_post
    and also looked at:
    http://codex.wordpress.org/wp_list_pages
    where it says how to list sub pages

  4. o3fingers
    Member
    Posted 6 years ago #

    I just want to ditto this question! I've been trying to figure out a way to get wp_list_pages to show the children of the current page and the parent of the current page with links, but I've had no luck... This must have already been done by hundreds of people, but I'm really new to this.

  5. roambe
    Member
    Posted 6 years ago #

    The line giving you the error is this:

    $parent_title = ($parent_id->post_title);

    The error is that you're trying to retrieve the post title from the parent id. What you need is this:

    $parent_title = ($parent_post->post_title);
    echo $parent_title;

    Notice: $parent_post, not $parent_id.

  6. gilliancs
    Member
    Posted 6 years ago #

    Thanks for your help: this makes sense and I'm a step further. Unfortunately it still gives me the same parse error.

Topic Closed

This topic has been closed to new replies.

About this Topic