Skip to content

Instantly share code, notes, and snippets.

@dannyfoo
Created April 11, 2022 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dannyfoo/576475b411785f6ab28bbecdd5ef056c to your computer and use it in GitHub Desktop.
Save dannyfoo/576475b411785f6ab28bbecdd5ef056c to your computer and use it in GitHub Desktop.
Add post thumbnail to Elementor post navigation
<?php
// Source: https://github.com/elementor/elementor/issues/3863#issuecomment-643794411
// Previous next post thumb
add_filter('previous_post_link', 'sydney_child_post_nav_thumbnail', 20, 5 );
add_filter('next_post_link', 'sydney_child_post_nav_thumbnail', 20, 5 );
function sydney_child_post_nav_thumbnail($output, $format, $link, $post, $adjacent) {
if( !$output ) {
return;
}
$divclass = '';
switch ($adjacent) {
case 'next':
$divclass = 'custom-nav nav-next';
break;
case 'previous':
$divclass = 'custom-nav nav-previous';
break;
default:
break;
}
$arrow_prev = '';
$arrow_next = '';
if( 'next' == $adjacent ) {
$arrow_next = '<span>&#10230;</span>';
}
if( 'previous' == $adjacent ) {
$arrow_prev = '<span>&#10229;</span>';
}
$rel = $adjacent;
$thumb = get_the_post_thumbnail($post->ID, array( 100, 100) );
$title = '<div class="'.$divclass.'">' . $arrow_prev . $post->post_title . $arrow_next . '</div>';
$class = '';
if( !empty($thumb) ) {
$class = 'post-nav-has-thumbnail';
}
$string = '<a href="' . get_permalink( $post->ID ) . '" rel="' . $rel . '" class="'.$class.'">' . $thumb;
$inlink = str_replace( '%title', $title, $link );
$inlink = $string . $inlink . '</a>';
$output = str_replace( '%link', $inlink, $format );
if( !$post->ID ) {
return;
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment