Underscores.me retrieve next / previous post thumbnail in post_nav function

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
} 

Related posts

2 comments

  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
    

    }

  2.  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' );
    

Comments are closed.