Get Next Post or First Post (2 posts)

  1. bremmerm
    Posted 3 years ago #

    I have the following code which should display the next post or the first post if it is the last post.

    $next = next_post_link('%link', 'Next Game');
      if($next == null){
    	echo '<a rel="first" href="'.get_permalink(4).'">Next Game</a>';
    	 echo next_post_link('%link', 'Next Game');

    It works well except on posts where it is NOT the last post t displays two links: NextGameNext Game

    linking to both the next post and the first post!

    On the last post it correctly displays a link to the first post. I can't understand how it is pulling two links for posts that are not the last post.

  2. Michael
    Forum Moderator
    Posted 3 years ago #


    next_post_link() outputs the result directly, and can therefore not be used as a string or in conditional statements.

    try to work with get_adjacent_post():

Topic Closed

This topic has been closed to new replies.

About this Topic