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 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.
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 5 years, 8 months ago.
Assisted by: Christian Cox.