WordPress.org

Forums

Previous and Next post links to always stay visible (4 posts)

  1. Brownrice
    Member
    Posted 2 years ago #

    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

  2. Andrew
    Forum moderator
    Posted 2 years ago #

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

  3. alchymyth
    Forum Moderator
    Posted 2 years ago #

    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

  4. Brownrice
    Member
    Posted 2 years ago #

    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>'; } ; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic