------ Tell us what you are trying to do?
I have a custom post type named 'Portfolios' where I am in it's content template 'T2 Portfolios (single)' trying to display the featured image to the next post, I am using this shortcode below:
[wpv-post-featured-image]
it is (like expected) displaying the featured image thumbnail to the current post.
how would I modify this shortcode to display the featured image thumbnail to the next post instead of the current post?
------ Is there any documentation that you are following?
https://toolset.com/documentation/user-guides/views-shortcodes/
Long story short it needs some custom code.
You can pass an ID to the Featured Image ShortCode like so:
[wpv-post-featured-image item="1"]
You can replace the 1 in the "item" attribute with a Custom ShortCode that returns the next post in the loop, using the native WordPress function get_next_post()
https://codex.wordpress.org/Function_Reference/get_next_post
A ShortCode that returns the next post using this function can be crafted using these guidelines.
https://codex.wordpress.org/Shortcode_API
Example:
function next_post_id( ) {
$next_post_id = get_next_post()->ID;
return $next_post_id;
}
add_shortcode( 'next_post_id', 'next_post_id' );
Then you can use that in the Featured Image ShortCode as:
[wpv-post-featured-image item="[next_post_id]"]
It will display the featured image of the next post in the loop (single post type template, for example)
Don't forget to register this ShortCode in Toolset > Settings > Front end content > Third-party shortcode arguments, or it'll not work.
this works great to pull the next post ID, thank you so much again!
im not good with .php, how could this shortcode be modified to loop back to the first post if it is currently at the last post?
Hello,
It needs custom codes, for example, you can modify the PHP codes as below:
function next_post_id() {
$next_post = get_next_post();
if(!$next_post){
$args = array(
'numberposts' => 1,
'post_type' => 'portfolios', // replace it with your post type slug
'order' => 'ASC',
'fields' => 'ids'
);
$first_post = get_posts( $args );
$next_post_id = $first_post[0];
}
else{
$next_post_id = get_next_post()->ID;
}
return $next_post_id;
}
add_shortcode( 'next_post_id', 'next_post_id' );
My issue is resolved now. Thank you! You guys are awesome!!
Hello Luo!
thanks again for resolving this for me, I am using this method now to not only call the featured image, but also some normal and custom fields for the next post, these are the shortcodes I am using:
- this is calling the featured image for the next post:
[types field='primary-portfolio-image' title='%%TITLE%%' alt='%%ALT%%' align='center' size='full' item='[next_post_id]'][/types]
- this is outputting a link for the next post:
[wpv-post-next-link item='[next_post_id]' format="%%LINK%%" link="NEXT PROJECT"]
- this is calling the title for the next post:
[wpv-post-next-link item='[next_post_id]' format="%%LINK%%" link="%%TITLE%%"]
- this is calling a custom field for the next post
[types field='location' item='[next_post_id]'][/types]
- this is calling a custom field for the next post
[types field='number' output='raw' item='[next_post_id]'][/types]
this is being used at the bottom of each individual custom post 'portfolio' as you can see at the bottom of this page:
hidden link
it seems to be working just fine across all my posts.
my problem is, i can't seem to figure out at the bottom of this post in particular
hidden link
why these two shortcodes below are not displaying,
[wpv-post-next-link item='[next_post_id]' format="%%LINK%%" link="NEXT PROJECT"]
[wpv-post-next-link item='[next_post_id]' format="%%LINK%%" link="%%TITLE%%"]
but the rest of the shortcodes are displaying just fine
thank you so much in advanced Luo!!
I figured it out, my shortcodes were all messed up, thank you Luo