How can I echo previous and next page link names in WordPress?

I am using this wonderful bit of code created by jackreichert.

This in the functions.php file.

Read More
function siblings($link) {
    global $post;
    $siblings = get_pages('child_of='.$post->post_parent.'&parent='.$post->post_parent);
    foreach ($siblings as $key=>$sibling){
        if ($post->ID == $sibling->ID){
            $ID = $key;
        }
    }
    $closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));

    if ($link == 'before' || $link == 'after') {
        echo $closest[$link];
    }
    else
    {
        return $closest;
    }
}

and this on the template page

<?php siblings('before'); ?>

and

<?php siblings('after'); ?>

It works wonderfully, but to the echo I would like to be able to add the name of the link as well. How could this be done?

Related posts

Leave a Reply

1 comment

  1. Change this line:

    $closest = array('before'=>get_permalink($siblings[$ID-1]->ID),'after'=>get_permalink($siblings[$ID+1]->ID));
    

    To:

    $closest = array('before'=> '<a href="'.get_permalink($siblings[$ID-1]->ID).'">'.get_the_title($siblings[$ID-1]->ID).'</a>','after'=> '<a href="'.get_permalink($siblings[$ID+1]->ID).'">'.get_the_title($siblings[$ID+1]->ID).'</a>');