WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have come across a problem whilst using the next_post_link() function. It seems that this function automatically echo's, for position reasons I need this to just return the link. Is there any available function or workaround that I can use to achieve this result?

share|improve this question
up vote 6 down vote accepted

try to work with http://codex.wordpress.org/Function_Reference/get_adjacent_post;


$next_post_link_url = get_permalink( get_adjacent_post(false,'',false)->ID ); 
$prev_post_link_url = get_permalink( get_adjacent_post(false,'',true)->ID );
share|improve this answer
Bear in mind this will only get the adjacent URL - you'll have to generate the HTML like next_post_link does yourself. – TheDeadMedic Jun 27 '11 at 14:57

If you take a look at the source, next_post_link is just a wrapper for adjacent_post_link.

Unfortunately, this function doesn't take any form of 'echo' parameter, so you'll either need to replicate the code in your own function & return the value, or catch it in an output buffer;

$next_post_link = ob_get_clean();
share|improve this answer

Actually, yes: just use get_next_posts_link(), using the same arguments.

The next_posts_lin() function simply echoes the returned value of get_next_posts_link().


Erm, nevermind. I mis-read the function name.

You could use get_next_post(), which returns a post object; then you could get the permalink from the returned ID object parameter:

$nextpost = `get_next_post( $args )`;
$nextpostid = $nextpost->ID;
$nextpostlink = get_permalink( $nextpostid );
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.