Skip to content

Instantly share code, notes, and snippets.

@humayunahmed8
Forked from leavlzi/the_post_navigation.php
Created October 15, 2020 19:41
Show Gist options
  • Save humayunahmed8/83181f79c44674560f60dfdfcdf109ea to your computer and use it in GitHub Desktop.
Save humayunahmed8/83181f79c44674560f60dfdfcdf109ea to your computer and use it in GitHub Desktop.
Use WordPress default function to get next and previous post title with permalink something like this:
<?php // FOR PREVIOUS POST
$prev_post = get_previous_post();
$id = $prev_post->ID ;
$permalink = get_permalink( $id );
?>
<?php // FOR NEXT POST
$next_post = get_next_post();
$nid = $next_post->ID ;
$permalink = get_permalink($nid);
?>
Then just use this variable in your HTML structure like below:
<div class="next-timeline">
<?php next_post_link( '%link', __( 'Next <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) ); ?>
<h2><a href="<?php echo $permalink; ?>"><?php echo $next_post->post_title; ?></a></h2>
</div>
<div class="previous-timeline">
<?php previous_post_link( '%link', __( '<span class="meta-nav">&larr;</span> Previous', 'twentyeleven' ) ); ?>
<h2><a href="<?php echo $permalink; ?>"><?php echo $prev_post->post_title; ?></a></h2>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment