Next & Previous Post Titles With Thumbnails (13 posts)

  1. Bane Unreinen
    Posted 6 years ago #

    I am building a custom theme and taking advantage of a relatively new feature in WordPress, post thumbnails (http://codex.wordpress.org/Post_Thumbnails). Since the documentation is still pending, I figured that others may find this useful too. Keep in mind that this is to be placed in the single.php file.

    Here is the code to grab the next post title & the thumbnail:

    <?php next_post_link('&laquo;&nbsp;%link','%title', TRUE); ?><br /><?php $nextPost = get_next_post(true); $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(150,150) ); echo $nextthumbnail; ?>

    And this is the code to grab the previous post title & the thumbnail:

    <?php prev_post_link('%link&nbsp;&raquo;','%title', TRUE); ?><br /><?php $prevPost = get_prev_post(true); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(150,150) ); echo $prevthumbnail; ?>

    I hope that this is useful, and remember that your theme must have post thumbnails activated in order to work.

  2. Bane Unreinen
    Posted 6 years ago #

    ok, so I have it working almost perfectly. But I am having a problem in preventing the thumbnail from showing up when there is no next/previous post; like the next/previous links do.
    I other words, when I'm at the last (or first) post of a category, it show the current post thumbnail. Any ides on how to correct this?

  3. Michael
    Forum Moderator
    Posted 6 years ago #

    test if the $nextPost / $prevPost variables are 'filled':

    <?php $nextPost = get_next_post(true); if($nextPost) { /*nextthumb*/ } ; ?>

    <?php $prevPost = get_previous_post(true); if($prevPost) { /*prevthumb*/ } ; ?>

  4. Bane Unreinen
    Posted 6 years ago #

    @alchymyth: thanks! worked like a charm.

  5. Patrick_O
    Posted 6 years ago #

    @ alchymyth,
    I've searched high and low for this solution, but could find it anywhere...
    Loads of thanks!

  6. lilqhgal
    Posted 6 years ago #

    This is awesome but for some reason my theme is throwing a Call to undefined function for prev_post_link in the second snippet above. Any ideas why?

  7. Mark / t31os

    Posted 6 years ago #

    I believe that should actually be previous_post_link.


  8. colinpeddle
    Posted 6 years ago #

    I had to make a couple adjustments to the code for the previous post call.

    <?php previous_post_link('%link »','%title', TRUE); ?>
    <?php $prevPost = get_previous_post(true); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(150,150) ); echo $prevthumbnail; ?>

    I've put them in bold.

  9. colinpeddle
    Posted 6 years ago #

    Also, perhaps it's the modifications I've made, but shouldn't the thumbnails also be linking to the next or previous post?

  10. Paul de Wouters
    Human Made
    Posted 5 years ago #

  11. itsbarry
    Posted 5 years ago #

    Cool tip!

    Colin this is how I made my thumbnails clickable:

    <div id="cooler-nav" class="navigation">
                    <?php $prevPost = get_previous_post(true);
    					if($prevPost) {?>
    					<div class="nav-box previous" style="float:left;">
                        <?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );}?>
                        <?php previous_post_link('%link',"$prevthumbnail  %title", TRUE); ?>
                    <?php $nextPost = get_next_post(true);
    					if($nextPost) { ?>
                    <div class="nav-box next" style="float:right;">
    					<?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); } ?>
    					<?php next_post_link('%link',"$nextthumbnail  %title", TRUE); ?>
                    </div><!--#cooler-nav div -->

    (I actually put html line breaks before %title but they don't appear in my code block^)

  12. jjkilpatrick
    Posted 5 years ago #

    When I get to the last post i'd like it to show a default image of a cross. Any idea of how to do this?


  13. ishotr
    Posted 5 years ago #

    This also works.

    <?php $prevPost = get_previous_post(true); $prevThumbnail = get_the_post_thumbnail($prevPost->ID, array(150,150) ); echo $nextthumbnail; ?><?php previous_post_link( '%link', $prevThumbnail ); ?>

    using it on the sidebar of for Celebrity is hotter

    it's clickable too.

Topic Closed

This topic has been closed to new replies.

About this Topic