I have a function for displaying the title of the top-level page of the current sub-level page. It's working quite well, but I can't figure out how to also link to the same top level page that is displayed.

function my_page_ancestor() {
    global $post;
    if( !is_front_page() ) {
        $parents = get_post_ancestors( $post );
        $top_parent_link = get_permalink( end($parents) );
        echo '<a href="' . $top_parent_link . '"><span class="pageAncestor">' . get_the_title( end($parents) ) . '</span></a>';

I've tried get_the_permalink() inside the function, but it just breaks.

Edit: I've corrected the function above, and it now works as I wanted.

1 Answer 1


You may want to try get_permalink() ie: get_permalink( end($parents) ). :-)

  • Oh that's embarrassing :-) Thanks for correcting me! Commented Apr 27, 2013 at 11:50
  • Indeed, that's a mean one. :-)
    – s1lv3r
    Commented Apr 27, 2013 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.