How to add thumbnail support to WordPress next / previous post loop?

Question

I’m using the following code snippet for the next/previous navigation loop in Single.php.

<?php 
     $nextPost = get_next_post();
    if($nextPost) {
        $nextPostID = $nextPost->ID;
?>
    <a class="prev-post" href="<?php echo get_permalink( $nextPostID ); ?>">
        <?php echo $nextPost->post_title; ?>
    </a>
<?php } else {
        $first_post = get_posts( array(
            'posts_per_page'   => 1,
            'order' => 'ASC'
        ) );
        ?>
        <a class="prev-post" href="<?php echo get_permalink( $first_post[0]->ID ); ?>">
            <?php echo get_the_title( $first_post[0]->ID ); ?>
        </a>

<?php } ?>

<?php
    $prevPost = get_previous_post();
    if($prevPost) {
        $prevPostID = $prevPost->ID;
?>
    <a class="next-post" href="<?php echo get_permalink( $prevPostID ); ?>">
        <?php echo $prevPost->post_title; ?>
    </a>
<?php } else {
    $latest_post = get_posts( array(
        'posts_per_page'   => 1,
        'order' => 'DESC'
    ) );
    ?>
    <a class="next-post" href="<?php echo get_permalink( $latest_post[0]->ID ); ?>">
        <?php echo get_the_title( $latest_post[0]->ID ); ?>
    </a>
<?php } ?>

The script is working fine but I want it to show post thumbnails with size option (small/medium/large). I tried adding ” but I have to repeat it twice within the link and in each instance while it looks like it is working but the HTML produced is much like var_dump than anything:

<img width="1024" height="683" src="http://localhost:8888/testsite/wp-content/uploads/2023/04/thumb-6-1600-1024x683.jpg" class="attachment-large size-large wp-post-image" alt="" decoding="async" loading="lazy"  >

0
NORDc 1 month 2023-04-29T03:54:46-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse