Display back to parent page link with get_post_ancestors

Adding the first snippet to the functions.php of your wordpress theme will display a link on sub pages back to the top level parent page. Place the second snippet within your theme in the location you want to display the link.

function wps_parent_post(){
global $post;
if ($post->post_parent){
$parent = $ancestors[$root];
} else {
$parent = $post->ID;
if($post->ID != $parent){
echo '<a href="'.get_permalink($parent).'" class="parent-post">Back to parent page</a>';


Leave a comment