WordPress.org

Support

Support » Themes and Templates » Get thumbail of next post

Get thumbail of next post

  • 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?

Viewing 2 replies - 1 through 2 (of 2 total)
  • 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’

    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>
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get thumbail of next post’ is closed to new replies.
Skip to toolbar