How to get next/previous post hrefs or URL and titles in WordPress - Codersandipdas

How to get next/previous post hrefs or URL and titles in WordPress -  Codersandipdas





<?php 
    
    $prev_post = get_adjacent_post(false, '', true);
    if(!empty($prev_post)) {
echo '<a class="float-left" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">prev</a>'; 
    }
    $next_post = get_adjacent_post(false, '', false);
    if(!empty($next_post)) {
    echo '<a class="float-right" href="' . get_permalink($next_post->ID) . '" title="' . $next_post->post_title . '">next</a>'; 
    }
?>

Note that get_adjacent_post is used to get previous and next posts, you can read about it here To get the previous post and its title attribute use this