WordPress.org

Forums

[resolved] Next & Previous Post Thumbnails (4 posts)

  1. jjkilpatrick
    Member
    Posted 4 years ago #

    I am building a custom theme and i'm using the post thumbnail function to show next and previous posts in my sidebar.

    Here is the code i'm using

    <div class="previmg">
    <?php $post_thumbnail = get_post_meta($post->ID, 'post_thumbnail', true);
    if ($post_thumbnail) {
    ?>
    <a href="<?php previous_post_link(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo $post_thumbnail; ?></a>
    
    <?php } else { ?>
    
    <?php $prevPost = get_previous_post(); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(66,66) ); previous_post_link('%link',''.$prevthumbnail.'', TRUE); ?>
    
    <?php } ?>
    
    </div>
    
    <div class="nextimg">
    <?php $post_thumbnail = get_post_meta($post->ID, 'post_thumbnail', true);
    if ($post_thumbnail) {
    ?>
    <a href="<?php next_post_link(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo $post_thumbnail; ?></a>
    
    <?php } else { ?>
    
    <?php $nextPost = get_next_post(true); $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(66,66) ); next_post_link('%link',''.$nextthumbnail.'', TRUE); ?>
    
    <?php } ?>
    </div>

    I am having a problem in showing a default thumbnail (for example a empty thumbnail with a cross) when you get to the very last post or the very first post.

    Any ides on how to do this?

  2. alchymyth
    Forum Moderator
    Posted 4 years ago #

    <div class="previmg">
    
    <?php if(get_adjacent_post(false, '', true)) : // check for the existance of a prev post ?>
    
    <?php $post_thumbnail = get_post_meta($post->ID, 'post_thumbnail', true);
    if ($post_thumbnail) {
    ?>
    <a href="<?php previous_post_link(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php echo $post_thumbnail; ?></a>
    
    <?php } else { ?>
    
    <?php $prevPost = get_previous_post(); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(66,66) ); previous_post_link('%link',''.$prevthumbnail.'', TRUE); ?>
    
    <?php } ?>
    
    <?php else : // if no prev post ?>
    
    <!--enter the code to show your crossed thumbnail-->
    
    <?php endif; // end of the prev post check ?>
    
    </div>

    the same way for next post:
    the conditional for the next post is:
    <?php if(get_adjacent_post(false, '', false)) : ?>

  3. jjkilpatrick
    Member
    Posted 4 years ago #

    Works perfectly. Thanks for your quick response.

  4. jjkilpatrick
    Member
    Posted 4 years ago #

    I was wondering if I could get some more support on this.

    Basically I've found when I get to the forth post it doesn't show a thumbnail. I've tried removing all posts and adding new ones but it still does the same.

    The link to my blog is http://itdrewitself.com/

    You'll see the next and prev on any single post.

    Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic