Skip Navigation

[Resolved] Displaying featured image of previous and next post on single post page

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to display "next post" and "previous post" links on my single post template. I would like to access the featured image of each of those posts to use in the links.

Solution: There's nothing built-in to Toolset that will do this for you, but for a very simple case I have a custom shortcode you can use. Add this to a new code snippet, or to your child theme's functions.php file:

function ts_get_adjacent_post_id_func($atts) {
  global $post;
  $atts = shortcode_atts([
    'previous' => false
  ], $atts);
  $adjacent = get_adjacent_post( false, '', $atts['previous']=='true');
  return isset( $adjacent->ID ) ? $adjacent->ID : '';
}
add_shortcode("ts_get_adjacent_post_id", "ts_get_adjacent_post_id_func");

Now you have a shortcode you can use like this:

Previous post featured image: [wpv-post-featured-image item="[ts_get_adjacent_post_id previous='true']"]
 
Next post featured image: [wpv-post-featured-image item="[ts_get_adjacent_post_id]"]

There is a quirk here you should be aware of. Basically on the first post, the previous ts_get_adjacent_post_id will return no value. However, if you put no value in the wpv-post-featured-image shortcode "item" attribute, the featured image from the current post will be displayed. Similarly on the last post, the next ts_get_adjacent_post_id shortcode will return no value. Therefore, you should use a conditional that tests whether the previous or next post ID is the empty. If so, then you should hide the previous or next featured image shortcode.

Relevant Documentation:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-featured-image
https://developer.wordpress.org/reference/functions/get_adjacent_post/

This support ticket is created 5 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by davidH-33 5 years, 1 month ago.

Assisted by: Christian Cox.

Author
Posts
#1210017
previous-next-1.jpg

Tell us what you are trying to do?
On the bottom of single post layout for Blog I need to display the navigation to previous and next post incl. the name of previous/next post and also the featured image for the previous and next post (the single post layout has been already made in Elementor pro). Is there any chance to prepare this in Toolset and place the shortcode to Elementor pro template?

Is there any documentation that you are following?
I know about [wpv-post-next-link] and [wpv-post-previous-link] shortcodes but I can't figure out how to use in shortcode the featured image for previous/next post (the image should be used as background image).

Is there a similar example that we can see?
Please see screenshot of how should final design look.

What is the link to your site?
Here is single post page with temporary solution - prev./next navigation without images (using 3rd party plugin): hidden link

#1210210

The featured image shortcode will accept a post ID attribute, which will help you show the featured image of another post.
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-featured-image

So you need a way to get the adjacent post ID given the current post ID. There's nothing built-in to Toolset that will do this for you, but for a very simple case I have a custom shortcode you can use. Add this to a new code snippet, or to your child theme's functions.php file:

function ts_get_adjacent_post_id_func($atts) {
  global $post;
  $atts = shortcode_atts([
    'previous' => false
  ], $atts);
  $adjacent = get_adjacent_post( false, '', $atts['previous']=='true');
  return isset( $adjacent->ID ) ? $adjacent->ID : '';
}
add_shortcode("ts_get_adjacent_post_id", "ts_get_adjacent_post_id_func");

Now you have a shortcode you can use like this:

Previous post featured image: [wpv-post-featured-image item="[ts_get_adjacent_post_id previous='true']"]

Next post featured image: [wpv-post-featured-image item="[ts_get_adjacent_post_id]"]

There is a quirk here you should be aware of. Basically on the first post, the previous ts_get_adjacent_post_id will return no value. However, if you put no value in the wpv-post-featured-image shortcode "item" attribute, the featured image from the current post will be displayed. Similarly on the last post, the next ts_get_adjacent_post_id shortcode will return no value. Therefore, you should use a conditional that tests whether the previous or next post ID is the empty. If so, then you should hide the previous or next featured image shortcode.

#1211232

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.