http://themethesis.com/wp-content/uploads/2010/07/previous-next-thumbnails.jpg

How to Use Thumbnails for Previous/Next Navigation in WordPress

July 6, 2010

in Tutorials

Previous Next Link Thumbnails

Almost every WordPress theme you’ll come across has support for going to the previous or next post. That’s all well and good, but in WordPress 2.9 we got the ability to have post thumbnails. Wouldn’t it be great to use thumbnails instead of or alongside our post titles? The answer is yes, it would. So I embarked on a journey to figure out how to add this functionality to your theme, and have posted the results below. First for regular WordPress users, and then for Thesis users.

  1. Add post thumbnail support

    Open up functions.php and place add_theme_support(‘post-thumbnails’); in there, if it isn’t there already.

  2. Add the code

    Open single.php and go down to where the previous_post_link and next_post_link is situated. If your theme doesn’t have that already, then just find a proper place for the thumbnails. Place the following code in:

    <?php
    $prevPost = get_previous_post();
    $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(150,150) );
    previous_post_link('%link',''.$prevthumbnail.'', TRUE);
    ?>
    
    <p class="prev_next2"><?php previous_post_link('%link','<') ; ?>
    
    <?php
    $nextPost = get_next_post();
    $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(150,150) );
    next_post_link('%link',''.$nextthumbnail.'', TRUE);
    ?>
    <p class="prev_next2"><?php previous_post_link('%link','>') ; ?></p>
    

    And that’s it for the code. The 150,150 is the size of the thumbnail, so feel free to experiment with that. You can style the thumbnail by targetting .wp-post-image in your css. The ‘<' can be changed to ‘%title’ if you’d like to put your previous/next titles in there instead of an arrow, and this can be styled by targeting p.prev_next2.

  3. Add post thumbnails to your post

    Go into your post screen on the dashboard, and make sure you have an image set for “Featured image” on the right hand size. This is where the thumbnail will come from.

  4. For Thesis users:

  5. The trick works the same for Thesis, only instead of placing it in single.php, place it in a function like follows:

    function prev_next(){
    if (is_single()) {
    ?>
    
    // CODE HERE
    
    <?php } }
    add_action('thesis_hook_after_content, 'prev_next);
    remove_action('thesis_hook_after_content','thesis_prev_next_posts');
    

    The remove_action gets rid of the previous/next navigation that is already built in.

Sources:

WordPress support forums
Digging into WordPress

Thesis Theme for WordPress:  Options Galore and a Helpful Support Community

You'll notice this site talks about the Thesis theme for Wordpress. Don't know what the heck that is? Find out.

{ 14 comments… read them below or add one }

Damian July 6, 2010 at 4:37 pm

When you say to place the code for Thesis in a function, where exactly would that go? I’m assuming it’s not custom functions because it’s not defined as such.

Thanks for your help — great site!

Reply

Matt Dunn July 6, 2010 at 4:54 pm

Yes, that’s custom functions. All functions for Thesis go in that file.

And thanks. :)

Reply

Nitin July 13, 2010 at 3:04 pm

Hi Matt,

Will this trick work on pages too? I use Thesis.

Reply

Matt Dunn July 13, 2010 at 11:31 pm

It will work in Thesis but it will not work on Pages. Pages don’t have a “previous” or “next”.

Reply

Laura July 15, 2010 at 1:36 am

hey there, thanks for the info, I don’t see a single.php within my thesis theme, am I missing something?

Thanks,

Laura

Reply

laura July 15, 2010 at 1:36 am

maybe I should read the full post next time…

got it, Thanks,

Reply

marie September 6, 2010 at 8:47 am

Hello,

I am testing it but it looks like those functions are not defined?
Is there something that I am missing?

Thank you, Marie

Reply

Matt Dunn September 10, 2010 at 3:33 pm

What do you mean the functions aren’t defined?

Reply

JohnintheUK October 11, 2010 at 8:20 pm

I had the same issue, the problem was when copy & pasting “add_theme_support(‘post-thumbnails’);” you get fancy apostrophes around “post-thumbnails”. To fix it just delete the apostrophes and replace them with a normal ‘.

Thanks for the post Matt.

Reply

Katie October 3, 2010 at 4:44 pm

My issue is that i want to add next page to my post so like example

info info info…
Next Page
I want to put nextpage at the bottom of my website so if i click that it will continue to the post but it has to to hidden i dont want the topic to show above.

Reply

andy September 16, 2011 at 10:35 am

great, this just messed up my functions file in thesis…its doesnt work, sir!

Reply

Ross November 2, 2011 at 2:50 pm

Thanks for this, but I did have to do some cleaning up to make it work.

First, you need to add a closing to the first previous link.
Second, I said the first previous link, because you have two of them.
Third, need to change the thumbnail from ‘true’ to ‘false’ to have it work correctly. Doesn’t show up some times, if you don’t.
Fourth, obviously need to change the ‘second’ previous link to ‘next_post_link’.
Here it is, all cleaned up. No need for ‘styling’ just cut and paste:

“ID, array(150,150) );
next_post_link(‘%link’, ”.$nextthumbnail.”, false);
?>

ID, array(150,150) );
previous_post_link(‘%link’, ”.$prevthumbnail.”, false);
?>

Remember to remove the ( ” ) at the beginning and end.

Reply

Ross November 2, 2011 at 2:52 pm

Sorry, it didn’t show all of it. Try this:

ID, array(150,150) );
next_post_link(‘%link’, ”.$nextthumbnail.”, false);
?>

ID, array(150,150) );
previous_post_link(‘%link’, ”.$prevthumbnail.”, false);
?>

Reply

Ross November 2, 2011 at 2:54 pm

I tried. Just need to make the changes, then you can delete all my comments….;)

Reply

Leave a Comment

Previous post:

Next post: