Created
April 11, 2022 09:14
-
-
Save dannyfoo/576475b411785f6ab28bbecdd5ef056c to your computer and use it in GitHub Desktop.
Add post thumbnail to Elementor post navigation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>⟶</span>'; | |
} | |
if( 'previous' == $adjacent ) { | |
$arrow_prev = '<span>⟵</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