Forums

previous_post_link() : placeholder if first post (5 posts)

  1. superuntitled
    Member
    Posted 2 years ago #

    I am having trouble with the previous_post_link() and next_post_link() functionality. When there is no previous post, the function previous_post_link() does not display a link, likewise for the next_post_link() and the last post. I would like to have a placeholder image so that the design stays consistent.

    Currently I have images of green arrows pointing left and right, I would like to place an image of a grey arrow if there are no more posts to go back to.

    Is there a way to use the next_post_link()/previous_post_link() functions but not have the link removed.

    I also wonder if there is a way for the links to cycle, so that if you come to the most recent post, the next post link would bring you back to the first post.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    there is this wordpress function get_adjacent_post() that can be used.
    here is the code i use in my themes to do a similar thing to what you want to do.

    as a general idea, the 'cycling' could probably be done with some queries (in the cases where you want to show a grey arrow), fetching the permalink to the oldest and latest published post, resp., and then rebuilding a link the same way as the output of 'prev/next post link'; that would depend on the parameters you use with 'prev/next post link'.

  3. giant slayer
    Member
    Posted 2 years ago #

    I would like to the same thing. But I am not sure how to determine if I am at the first post or the last post.

    I tried

    <?php if(previous_post_link()){
    	previous_post_link('&laquo; %link', 'Previous Post: %title', TRUE);
    }else{do something} ?>

    I also tried

    <?php $prev=previous_post_link();
    if($prev=NULL){
    	previous_post_link('&laquo; %link', 'Previous Post: %title', TRUE);
    }else{do something} ?>

    Neither returned a null value.

  4. giant slayer
    Member
    Posted 2 years ago #

    I think you can do what you want with multiple loops.

    Look at the following code for an idea.

    $categories = get_the_category();
    $category = $categories[0];
    $catcount = $categories[0]->category_count;
    rewind_posts();
    query_posts('cat='.strval($category->cat_ID) . "&order=ASC");
    $navcount=1;
    while(have_posts()) : the_post();
    	if ($navcount==1){ ?>
    	    <a href="<?php the_permalink()?>" title="<?php the_title() ?>"><?php echo 'First'; ?></a>&nbsp;
      <?php }elseif($navcount==$catcount){ ?>
    	    <a href="<?php the_permalink()?>" title="<?php the_title() ?>"><?php echo 'Last'; ?></a>&nbsp;
      <?php }
    $navcount++;
    endwhile;
  5. giant slayer
    Member
    Posted 2 years ago #

    Of course, you will need your opening and closing php tags.

Topic Closed

This topic has been closed to new replies.

About this Topic