2

I am using Underscores.me for a project. I'd like to add the featured image (thumbnail) to the previous / next post links. They have a function which I'm trying to edit called THEMENAME_post_nav(). I am struggling to tweak this function to make it include the thumbnails. Can anyone help please? Here's the code:

function THEMENAME_post_nav() {
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
$next     = get_adjacent_post( false, '', false );

// This is how I'm trying to include the thumbnails
$prevthumbnail = get_the_post_thumbnail($previous->ID);
$nextthumbnail = get_the_post_thumbnail($next->ID);

if ( ! $next && ! $previous ) {
    return;
}
?>
<nav class="navigation post-navigation" role="navigation">
    <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'THEMENAME' ); ?></h1>
    <div class="nav-links">
        <?php

            previous_post_link( '<div class="nav-previous">%link</div>$prevthumbnail', _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', 'THEMENAME' ) );
            next_post_link( '<div class="nav-next">%link</div>$nextthumbnail', _x( '%title <span class="meta-nav">&rarr;</span>', 'Next post link', 'THEMENAME' ) );
        ?>
    </div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
} 

2 Answers 2

1

I solved the problem by using get_the_post_thumbnail() instead of the variables $prevthumbnail and $nextthumbnail.

function THEMENAME_post_nav() {
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
$next     = get_adjacent_post( false, '', false );

if ( ! $next && ! $previous ) {
    return;
}
?>
<nav class="navigation post-navigation" role="navigation">
    <!-- <h1 class="screen-reader-text"><?php _e( 'Post navigation', 'THEMENAME' ); ?></h1> -->
    <div class="nav-links">
        <?php
            previous_post_link( '<div class="nav-previous">' . get_the_post_thumbnail() . '%link</div>', _x( '%title', 'Previous post link', 'THEMENAME' ) );
            next_post_link(     '<div class="nav-next">' . get_the_post_thumbnail() . '%link</div>',  _x( '%title', 'Next post link',     'THEMENAME' ) );
        ?>
    </div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php

}

1
  • Hi there, if you're still around, you can accept this answer even though it is your own - there will be a check mark next to it on the left you can click :)
    – Tim Malone
    Commented May 13, 2016 at 5:33
0
 previous_post_link( '<div class="nav-previous">%link</div>$prevthumbnail', _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', 'THEMENAME' ) );
 next_post_link( '<div class="nav-next">%link</div>$nextthumbnail', _x( '%title   <span class="meta-nav">&rarr;</span>', 'Next post link', 'THEMENAME' );

It seems to me that you have unintendedly put the variables $prevthumbnail and $nexxtthumbnail inside single quotes.

Following what I just stated, I would try this:

previous_post_link( '<div class="nav-previous">%link</div>'. $prevthumbnail, _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', 'THEMENAME' ) );
next_post_link( '<div class="nav-next">%link</div>' . $nextthumbnail, _x( '%title   <span class="meta-nav">&rarr;</span>', 'Next post link', 'THEMENAME' );
1
  • you were correct about where the code should be placed. The variables I was using kept throwing errors though, I guess I was doing that part incorrectly. Please see my answer. Thanks for the help.
    – mtwallet
    Commented Feb 12, 2014 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.