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.


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

| improve this answer | |
  • Oh that's embarrassing :-) Thanks for correcting me! – Cody MacPixelface Apr 27 '13 at 11:50
  • Indeed, that's a mean one. :-) – s1lv3r Apr 27 '13 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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