php – How to add thumbnail to the navigation for previous/next post-ThrowExceptions

Exception or error:

1) I have this code, which allows me to add navigation of the titles of the posts: the next and previous one. So I can move between them quickly.

function get_the_post_navigation( $args = array() ) {
// Make sure the nav element has an aria-label attribute: fallback to the screen reader text.
if ( ! empty( $args['screen_reader_text'] ) && empty( $args['aria_label'] ) ) {
    $args['aria_label'] = $args['screen_reader_text'];
}

$args = wp_parse_args(
    $args,
    array(
        'prev_text'          => '%title',
        'next_text'          => '%title',
        'in_same_term'       => false,
        'excluded_terms'     => '',
        'taxonomy'           => 'category',
        'screen_reader_text' => __( 'Post navigation' ),
        'aria_label'         => __( 'Posts' ),
    )
);

$navigation = '';

$previous = get_previous_post_link(
    '<div class="nav-previous">%link</div>',
    $args['prev_text'],
    $args['in_same_term'],
    $args['excluded_terms'],
    $args['taxonomy']
);

$next = get_next_post_link(
    '<div class="nav-next">%link</div>',
    $args['next_text'],
    $args['in_same_term'],
    $args['excluded_terms'],
    $args['taxonomy']
);

// Only add markup if there's somewhere to navigate to.
if ( $previous || $next ) {
    $navigation = _navigation_markup( $previous . $next, 'post-navigation', $args['screen_reader_text'], $args['aria_label'] );
}

return $navigation;

}

2) I can implement this code directly to add thumbnails to the titles of the posts:

    <?php

if ( is_singular('work') ) {
    //Get the thumnail url of the previous and next post
    $prevThumb = get_the_post_thumbnail_url( get_previous_post(), 'full' );
    $nextThumb = get_the_post_thumbnail_url( get_next_post(), 'full' );

    //Get the links to the Previous and Next Post
    $previous_link_url = get_permalink( get_previous_post() );
    $next_link_url = get_permalink( get_next_post() );

    //Get the title of the previous post and next post
    $prevTitle = get_the_title( get_previous_post() );
    $nextTitle = get_the_title( get_next_post() );
}

 ?>

<div class="post-navigation">
    <a class="post-nav" href="<?php echo "$previous_link_url"; ?>">
        <div class="background-image" style="background: url('<?php echo $prevThumb; ?>');">
            <h2 class="highlight">&larr; <?php echo "$prevTitle"; ?></h2>
            <div class="overlay"></div>
        </div>
    </a>


    <a class="post-nav" href="<?php echo "$next_link_url"; ?>">
        <div class="background-image" style="background: url('<?php echo $nextThumb; ?>');">
            <h2 class="highlight"><?php echo "$nextTitle"; ?> &rarr;</h2>
            <div class="overlay"></div>
        </div>
    </a>
</div>

What I want to do is to implement the code (2) into the function (1) get_the_post_navigation.

Is there anyone who knows how to achieve this goal?


I could also add the code with thumbnails to the titles with the javascript code, but I feel like it could be done with modifying the function ‘get_the_post_navigation’. So every time I use the function ‘What I want to do is to implement the code (2) into the function (1) get_the_post_navigation. I’d like to be able to use the function to display not only the titles of the posts but also the thumbnails.

Is there anyone who knows how to achieve this goal?


I could also add the code with thumbnails to the titles with the javascript code, but I feel like it could be done with modifying the function ‘get_the_post_navigation’.

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *