• Resolved lynkei

    (@lynkei)


    Hello,
    I am using the following code to create a link to the parent of a page.

    <?php $permalink = get_permalink($post->post_parent); ?>
    <a href="<?php echo $permalink; ?>" style="text-decoration: none;" class="bauhaus size20px colororange">?<?php
    $parent_title = get_the_title($post->post_parent);
    echo $parent_title;
    ?></a>

    I’m wondering what I should modify or how I should go about making it display the parent of that parent.

    iow, my page structure is like this:

    Page 1
    -Page 1A
    –Page 1A-a

    When I am on Page 1A-a I would like to make a link to page 1.

    Thanks in advance for your help.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi lynkei,

    You already have parent page ID (Page 1A):

    $parent_id = $post->post_parent;

    Now, you can use that to get parent page object:

    $parent = get_post( $parent_id );

    And grandparent’s ID would be:

    $grandparent_id = $parent->post_parent;

    Or you can use get_post_ancestors function – https://codex.www.ads-software.com/Function_Reference/get_post_ancestors

    Thread Starter lynkei

    (@lynkei)

    Thanks “Slobodan Manic” for taking the time to look at this.

    My knowledge of php is somewhat limited. Could you point me to where I would insert that code?

    Thanks

    The same page you tried that first block of code should work.

    You can get grandparent page title by using get_the_title( $grandparent_id )

    Thread Starter lynkei

    (@lynkei)

    Right, I assumed that.

    Let me reword my question. Where in that block of code would I insert your code?

    Sorry for not making my question more clear.
    Thanks

    No, I’m sorry, I should’ve been more clear as well ??

    So, if you add this block of code before the block of code you originally shown:

    <?php
    $parent_id = $post->post_parent;
    $parent = get_post( $parent_id );
    $grandparent_id = $parent->post_parent;
    ?>

    Then in your block of code, you can use get_the_title( $grandparent_id ) or get_permalink( $grandparent_id) and those will get you grandparent page title and permalink.

    Thread Starter lynkei

    (@lynkei)

    Ah, that did it.
    Thanks a lot.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘link to parent's parent’ is closed to new replies.