WordPress.org

Support

Support » How-To and Troubleshooting » Previous and Next post links to always stay visible

Previous and Next post links to always stay visible

  • Hello

    How would I get the links to stay visible even when there is not a previous or next post. I would like the links to navigate in a cycle.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Andrew Nevins

    @anevins

    Forum moderator

    Can you post the code that displays these links, in pastebin.com?

    just some related code from my test site (from single.php):

    <div class="navigation">			
    
    	<div class="alignleft">
    	<?php if(get_adjacent_post(false, '', true)) {  echo 'Previous older article: '; previous_post_link('%link');   }
     else { echo '<span style="color:#bbb;">"'.get_the_title().'" is the oldest post: ';
     $newest_post = get_posts('numberposts=1&order=DESC&orderby=date');  $newest_post_id = $newest_post[0]->ID;
     echo '<a href="'.get_permalink($newest_post_id).'" title="link to '.get_the_title($newest_post_id).'"> continue with latest post.</a></span>'; } ?>
     </div>
    	<div class="alignright">
    	<?php if(get_adjacent_post(false, '', false)) {  echo 'Next newer article: '; next_post_link('%link'); }
     else { echo '<span style="color:#bbb;">"'.get_the_title().'" is the latest post: '; $eldest_post = get_posts('numberposts=1&order=ASC&orderby=date');  $eldest_post_id = $eldest_post[0]->ID;
     echo '<a href="'.get_permalink($eldest_post_id).'" title="link to '.get_the_title($eldest_post_id).'">- continue with oldest post.</a></span>'; } ; ?>
    	</div>
    
    </div>

    see if you can find something useful in there (not optimized)

    http://codex.wordpress.org/Function_Reference/get_adjacent_post
    http://codex.wordpress.org/Function_Reference/get_permalink
    http://codex.wordpress.org/Template_Tags/get_posts
    http://codex.wordpress.org/Function_Reference/next_post_link
    http://codex.wordpress.org/Function_Reference/previous_post_link

    Thanks for the code al,

    I am a part time web developer and am just starting out with PHP, I played arround with your code until I got it to do what I wanted. I am not sure if it is valid, I am not getting any errors – is there a php validator?

    I tried to cut out anything I would not need from the code. I put a graphic in place of the text links – not sure if the <img> tags are in correctly? I tried to make sense of the code, but as I said I am just starting out with PHP. You can see it working on our dev site: dev dot indiability dot org / news

    What do you mean by optimized?

    Below is used in index.php – I did minor variations in archive.php and single.php

    <?php if(get_adjacent_post(false, '', true)) {  echo previous_post_link('%link' , '<img class="prev-page" id="prev-arrow" src="' . get_bloginfo('stylesheet_directory') . '/graphics/arrow.jpg" />');  }
     else { echo previous_post_link('%link' , '<img class="prev-page" id="prev-arrow" src="' . get_bloginfo('stylesheet_directory') . '/graphics/arrow.jpg" /> ');
     $newest_post = get_posts('numberposts=1&order=DESC&orderby=date');  $newest_post_id = $newest_post[0]->ID;
     echo '<a href="'.get_permalink($newest_post_id).'">
    </a>'; } ?>
    <?php if(get_adjacent_post(false, '', false)) {  echo next_post_link('%link' , '<img class="next-arrow" src="' . get_bloginfo('stylesheet_directory') . '/graphics/arrow.jpg" />'); }
     else { echo ''; $eldest_post = get_posts('numberposts=1&order=ASC&orderby=date');  $eldest_post_id = $eldest_post[0]->ID;
     echo '<a href="'.get_permalink($eldest_post_id).'" title="link to '.get_the_title($eldest_post_id).'"><img class="next-arrow" src="' . get_bloginfo('stylesheet_directory') . '/graphics/arrow.jpg" /></a>'; } ; ?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Previous and Next post links to always stay visible’ is closed to new replies.
Skip to toolbar