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).

Code

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);
ob_start();
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);
ob_start();
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');

Usage

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

Personal tools
Namespaces
Variants
Actions
iThemes Codex
Codex Navigation
Toolbox