Got it.
Now this is my code:
$p = get_adjacent_post(1, '', 1);
if(!empty($p)) echo '<a class="prevpost" href="'.$p->guid.'" title="'.$p->post_title.'"> </a>';
$n = get_adjacent_post(1, '', 0);
if(!empty($n)) echo '<a class="nextpost" href="'.$n->guid.'" title="'.$n->post_title.'"> </a>';
The function returns an object of the prev/next post which I can use for generating my links. The first parameter is for restricting the post on the same cat.
I searched in wordpress codex a few times yesterday but didn't come across this function, now stumled upon it by accident.
If someone has a better/simpler/faster method please post to get an accepted answer.
<?
echo '<a href="'.get_permalink( get_the_ID()-1 ).'" title="'.get_the_title( get_the_ID()-1 ).'">Previous</a>';
echo '<a href="'.get_permalink( get_the_ID()+1 ).'" title="'.get_the_title( get_the_ID()-1 ).'">Next</a>';
?>
No need for functions and filters all you need to do is to use get_adjacent_post
instead of next_post_link
and prev_post_link
, Note that get_adjacent_post
is used to get previous and next post, you can read about it here
To get previous post and it's title attribute use this
$prev_post = get_adjacent_post(false, '', true);
if(!empty($prev_post)) {
echo '<a href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>'; }
To get next post and it's title attribute use this
$next_post = get_adjacent_post(false, '', false);
if(!empty($next_post)) {
echo '<a href="' . get_permalink($next_post->ID) . '" title="' . $next_post->post_title . '">' . $next_post->post_title . '</a>'; }