Hi Dorian,
Thank you for waiting, while I performed some tests.
To update the custom shortcode so that it works with WPML translated posts, you can replace the old shortcode with this one:
add_shortcode('show_custom_next_previous_links', 'show_custom_next_previous_links_func');
function show_custom_next_previous_links_func($atts) {
$a = shortcode_atts( array(
'view' => '',
), $atts );
$current_post_ID = get_the_ID();
$view_output = do_shortcode('[wpv-view name="'.$a['view'].'"]');
$view_output_arr = explode(",", $view_output);
ob_start();
if(!empty($view_output_arr)) {
$found_key = array_search($current_post_ID, $view_output_arr);
$prev_key = $found_key-1;
$next_key = $found_key+1;
if($prev_key >= 0) {
$prev_name = do_shortcode('[types field="mentor-first-name" item="'.$view_output_arr[$prev_key].'"][/types] [types field="mentor-last-name" item="'.$view_output_arr[$prev_key].'"][/types]');
$prev_link = '<a href="'.get_the_permalink($view_output_arr[$prev_key]).'" rel="prev">'.do_shortcode('[wpml-string context="wpv-views"]« previous[/wpml-string]').' - '.$prev_name.'</a>';
}
if($next_key < count($view_output_arr)) {
$next_name = do_shortcode('[types field="mentor-first-name" item="'.$view_output_arr[$next_key].'"][/types] [types field="mentor-last-name" item="'.$view_output_arr[$next_key].'"][/types]');
$next_link = '<a href="'.get_the_permalink($view_output_arr[$next_key]).'" rel="next">'.$next_name.' - '.do_shortcode('[wpml-string context="wpv-views"]next »[/wpml-string]').'</a>';
}
if( ($prev_link) || ($next_link) ) {
echo '<div class="custom-pagination">';
echo '<div class="custom-pagination-prev">'.$prev_link.'</div>';
echo '<div class="custom-pagination-next">'.$next_link.'</div>';
echo '</div>';
}
}
return ob_get_clean();
}
This shortcode will need a post view to get the IDs of the posts, instead of the "get_posts" function, because a post view will automatically update the results, based on the current language.
You'll create a new post view for your post type for which you'd like to use these next and previous links and set it to be ordered by the "mentor-last-name" field.
In the view's "Loop Editor" wizard, you'll select "List with separators" format for the output and only include the post ID shortcode so that this view's output only consists of the comma-separated list of post IDs:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop>
[wpv-item index=other]
[wpv-post-id],
[wpv-item index=last]
[wpv-post-id]
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found]
<strong>[wpml-string context="wpv-views"]No items found[/wpml-string]</strong>
[/wpv-no-items-found]
[wpv-layout-end]
The last step would be to include the custom shortcode in the content template where you'd like to show the next and previous links:
( the custom CSS code will remain the same as shared in the last message )
[show_custom_next_previous_links view="new-view-slug"]
Note: You'll replace "new-view-slug" with the actual name or slug of the newly created post view.
Important note: The custom code snippets that we provide are just to share some ideas that can help in getting started in the right direction. But it won't be possible for us to continue providing 1-1 customization assistance, over the forum.
( ref: https://toolset.com/toolset-support-policy/ )
For more personalized assistance around custom code, you can consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/
regards,
Waqar