• 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);

Viewing 5 replies - 1 through 5 (of 5 total)
  • Cathy Mitchell

    (@multitalentedmommy)

    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.

    Thread Starter gilliancs

    (@gilliancs)

    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:
    https://codex.www.ads-software.com/Function_Reference/get_post
    and also looked at:
    https://codex.www.ads-software.com/wp_list_pages
    where it says how to list sub pages

    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.

    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.

    Thread Starter gilliancs

    (@gilliancs)

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘link to the parent of the current post.’ is closed to new replies.