WordPress.org

Ready to get started?Download WordPress

Forums

Get thumbail of next post (3 posts)

  1. NielsO
    Member
    Posted 3 years ago #

    I'm having some trouble getting reliable results from some code I wrote to get the thumbnail of the next and previous post in the single post template (single.php) to use for navigation. The posts being retrieved seem to not follow the post chronology.

    I suspect the get_adjacent_post function but I can't figure out a workaround, nor find any documentation online.

    Here is the code:

    <?php
    // Newer posts
    $nails_next_post = get_adjacent_post(false, '', false) ;
    $nails_next_post_id = $nails_next_post->ID;
    $nails_next_post_thumbnail = get_the_post_thumbnail($nails_next_post_id); // Get thumbnail
    ?>
    
    <?php if ($nails_next_post != null) : ?>
    <div class="post-nav-next">
    
       <?php if ($nails_next_post_thumbnail != ''): ?>
       <?php echo $nails_next_post_thumbnail; ?>
       <?php else : ?>
       <img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
       <?php endif; ?>
    
    <?php next_post_link('%link', 'Forward', FALSE, 3 ); ?>
    
    </div>
    <?php endif; ?>
    
    <?php
    // Older posts
    $nails_prev_post = get_adjacent_post(true, '', true) ;
    $nails_prev_post_id = $nails_prev_post->ID;
    $nails_prev_post_thumbnail = get_the_post_thumbnail($nails_prev_post_id); // Get thumbnail
    ?>
    
    <?php if ($nails_prev_post != null) : ?>
    <div class="post-nav-previous">
    
       <?php if ($nails_prev_post_thumbnail != ''): ?>
       <?php echo $nails_prev_post_thumbnail; ?>
       <?php else : ?>
       <img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
       <?php endif; ?>
    
       <?php previous_post_link('%link', 'Back', FALSE, 3 ); ?>
    
    </div>
    <?php endif; ?>

    Anyone have any suggestions?

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

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

    in 'next' post, you are using:

    $nails_next_post = get_adjacent_post(false, '', false) ;

    which has the 'in-same-cat' parameter set to 'false' - which is fine.

    contrary to this, in 'prev' post, you are using:

    $nails_prev_post = get_adjacent_post(true, '', true) ;

    which has the 'in-same-cat' parameter set to 'true' - which only retrieves a post in the same cat - which is different.

    suggestion:
    the first parameter in the get_adjacent_post() should be the same for both 'next' and 'prev'

  3. NielsO
    Member
    Posted 3 years ago #

    Oh wow! I feel dumb now! Thanks for taking the time alchymyth :-)
    For those that wish to use this snippet in the future here is the code in its generic entirety:

    <!-- Use this navigation code (only) in your single.php template -->
    
    <div id="post-nav">
    
    <?php
    // Newer posts
    $postnav_next_post = get_adjacent_post(false, '', false) ;
    $postnav_next_post_id = $postnav_next_post->ID;
    $postnav_next_post_thumbnail = get_the_post_thumbnail($postnav_next_post_id);
    ?>
    
    <?php if ($postnav_next_post != null) : ?>
    
    <div class="post-nav-next">
    
       <?php if ($postnav_next_post_thumbnail != ''): ?>
          <?php echo $postnav_next_post_thumbnail; ?>
       <?php else : ?>
           <img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
       <?php endif; ?>
    
       <?php next_post_link('%link', 'Forward', FALSE, 3 ); ?>
    
    </div>
    
    <?php endif; ?>
    
    <?php
    // Older posts
    $postnav_prev_post = get_adjacent_post(false, '', true) ;
    $postnav_prev_post_id = $postnav_prev_post->ID;
    $postnav_prev_post_thumbnail = get_the_post_thumbnail($postnav_prev_post_id);
    ?>
    
    <?php if ($postnav_prev_post != null) : ?>
    
    <div class="post-nav-previous">
    
       <?php if ($postnav_prev_post_thumbnail != ''): ?>
         <?php echo $postnav_prev_post_thumbnail; ?>
       <?php else : ?>
         <img src="<?php bloginfo('template_directory'); ?>/images/default-90x90.gif" />
       <?php endif; ?>
    
       <?php previous_post_link('%link', 'Back', FALSE, 3 ); ?>
    
    </div>
    
    <?php endif; ?>
    
    </div>

Topic Closed

This topic has been closed to new replies.

About this Topic