Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MichaelVanDenBerg/13e48ee92f6d99a806a71b5afb5caf34 to your computer and use it in GitHub Desktop.
Save MichaelVanDenBerg/13e48ee92f6d99a806a71b5afb5caf34 to your computer and use it in GitHub Desktop.
/**
* Add featured image as background image to post navigation elements.
*
* @see wp_add_inline_style()
*/
function alpha_centauri_post_nav_background() {
if ( ! is_single() ) {
return;
}
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
$css = '';
if ( is_attachment() && 'attachment' == $previous->post_type ) {
return;
}
if ( $previous && has_post_thumbnail( $previous->ID ) ) {
$prevThumb = wp_get_attachment_image_src( get_post_thumbnail_id( $previous->ID ), 'alpha-centauri-navigation-img' );
$css .= '
.post-navigation .nav-previous { background-image: url(' . esc_url( $prevThumb[0] ) . '); }
';
}
if ( $next && has_post_thumbnail( $next->ID ) ) {
$nextThumb = wp_get_attachment_image_src( get_post_thumbnail_id( $next->ID ), 'alpha-centauri-navigation-img' );
$css .= '
.post-navigation .nav-next { background-image: url(' . esc_url( $nextThumb[0] ) . '); }
';
}
wp_add_inline_style( 'alpha-centauri-style', $css );
}
add_action( 'wp_enqueue_scripts', 'alpha_centauri_post_nav_background' );
// Previous/next post navigation.
the_post_navigation( array(
'next_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Next', 'alpha-centauri' ) . '</span> ' . '<span class="screen-reader-text">' . __( 'Next post:', 'alpha-centauri' ) . '</span> ' . '<span class="post-title">%title</span>',
'prev_text' => '<span class="meta-nav" aria-hidden="true">' . __( 'Previous', 'alpha-centauri' ) . '</span> ' . '<span class="screen-reader-text">' . __( 'Previous post:', 'alpha-centauri' ) . '</span> ' . '<span class="post-title">%title</span>',
) );
@snecz
Copy link

snecz commented Apr 19, 2017

Hello, I'm trying to implement this into my Twenty Sixteen child theme but can't get it work. Do you have any idea why can it be? I have added this function into functions.php of my child theme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment