WordPress.org

Ready to get started?Download WordPress

Forums

Get Next Post or First Post (2 posts)

  1. bremmerm
    Member
    Posted 2 years ago #

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

    <?php
    $next = next_post_link('%link', 'Next Game');
    
      if($next == null){
    
    	echo '<a rel="first" href="'.get_permalink(4).'">Next Game</a>';
    
      }else{
    
    	 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. alchymyth
    Forum Moderator
    Posted 2 years ago #

    http://codex.wordpress.org/Function_Reference/next_post_link

    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():
    http://codex.wordpress.org/Function_Reference/get_adjacent_post

Topic Closed

This topic has been closed to new replies.

About this Topic