Add Previous and Next Title Links on Single Post Pages

From iThemes Codex
Jump to: navigation, search

If you want to show a Previous or Next Post Title link on a single post page, you can do so using a shortcode. Add the following code at the end of your functions.php file (but before the closing ?>, if any).


function my_previous_next_post() {
// retrieve the value for next post link
$next_post = get_adjacent_post(false, '', false) ;
$next_string = get_the_title($next_post->ID);
next_post_link("%link", $next_string);
$next_link = ob_get_clean();

// retrieve the value for previous post link
$previous_post = get_adjacent_post(true, '', true) ;
$previous_string = get_the_title($previous_post->ID);
previous_post_link("%link", $previous_string);
$previous_link = ob_get_clean();

// build output
$return = PHP_EOL . '<div id="next-previous" class="navigation clearfix">' . PHP_EOL;
// display previous link if any
if ($previous_link) {
         $return .= '<div class="nav-previous alignleft">'. PHP_EOL;
         $return .= $previous_link. PHP_EOL;
         $return .= '</div>'. PHP_EOL;
// display next link if any
if ($next_link) {
         $return .= '<div class="nav-next alignright">'. PHP_EOL;
         $return .= $next_link . PHP_EOL;
         $return .= '</div>'. PHP_EOL;
$return .= '</div>';
return $return;
add_shortcode('previous-next-post-links', 'my_previous_next_post');


Insert the shortcode [previous-next-post-links] in your LoopBuddy layout where you want the post title Previous and Next post links to appear.

← Back to LoopBuddy Codex Home