Hi
here comes version 2.
I made a new version that loads the previous and the next posts’ data, so you don’t need to have two code snippets anymore.
Also, you can now give an argument to the function, the argument being what post data you want to get:
pp_id = the ID of the previous post
pp_title = the title of the previous post
pp_thumb = the thumbnail image of the previous post
pp_link = the link to the previous post
np_id = the ID of the next post
np_title = the title of the next post
np_thumb = the thumbnail image of the next post
np_link = the link to the next post
How to use it in the Bricks editor:
For example, if you want to make a button that links to the previous post, enter the code
{echo:get_prev_and_next_post(‘pp_link’)}
into the Dynamic Data field:
If you want to show the thumbnail of the next post, enter this code
{echo:get_prev_and_next_post(‘np_thumb’)}
and
{echo:get_prev_and_next_post(‘np_link’)}
into these field in the image element:
If you want to show the title of the previous post in a heading element, enter this code
{echo:get_prev_and_next_post(‘pp_title’)}
and
{echo:get_prev_and_next_post(‘pp_link’)}
into these field in the heading element:
An icon with a link to the previous post would look like this:
I think you get how it works…
Here is the new code:
<?php
function get_prev_and_next_post($arg) {
$prev_post = get_adjacent_post();
$next_post = get_adjacent_post(false,"", false);
if ( ! empty( $prev_post ) ):
switch ($arg) {
case "pp_id": $returnarg = $prev_post->ID; break;
case "pp_title": $returnarg = wp_trim_words( $prev_post->post_title, 6 ); break;
case "pp_thumb": $returnarg = get_the_post_thumbnail_url( $prev_post->ID, 'thumbnail'); break;
case "pp_link": $returnarg = get_permalink( $prev_post->ID ); break;
}
endif;
if ( ! empty( $next_post ) ):
switch ($arg) {
case "np_id": $returnarg = $next_post->ID; break;
case "np_title": $returnarg = wp_trim_words( $next_post->post_title, 6 ); break;
case "np_thumb": $returnarg = get_the_post_thumbnail_url( $next_post->ID, 'thumbnail'); break;
case "np_link": $returnarg = get_permalink( $next_post->ID ); break;
}
endif;
if ( ! empty( $returnarg ) ):
return $returnarg;
endif;
}
Cheers
Patric