is it possible to get next_post_link

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?

Related posts

Leave a Reply

3 comments

  1. 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;

    ob_start();
    next_post_link();
    $next_post_link = ob_get_clean();
    
  2. 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().

    EDIT

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