Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add featured images to next/prev links.
if ( ! function_exists( 'generate_content_nav' ) ) :
/**
* Display navigation to next/previous pages when applicable
*/
function generate_content_nav( $nav_id ) {
global $wp_query, $post;
// Don't print empty markup on single pages if there's nowhere to navigate.
if ( is_single() ) {
$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous ) {
return;
}
}
// Don't print empty markup in archives if there's only one page.
if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) ) {
return;
}
$nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
$category_specific = apply_filters( 'generate_category_post_navigation', false );
?>
<nav id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
<h6 class="screen-reader-text"><?php _e( 'Post navigation', 'generatepress' ); ?></h6>
<?php if ( is_single() ) : // navigation links for single posts
$prevPost = get_previous_post();
$prevThumbnail = isset( $prevPost ) ? get_the_post_thumbnail( $prevPost->ID ) : '';
$nextPost = get_previous_post();
$nextThumbnail = isset( $nextPost ) ? get_the_post_thumbnail( $nextPost->ID ) : '';
previous_post_link( '<div class="nav-previous"><span class="prev" title="' . __('Previous','generatepress') . '">%link</span></div>', $prevThumbnail . '%title', $category_specific );
next_post_link( '<div class="nav-next"><span class="next" title="' . __('Next','generatepress') . '">%link</span></div>', $nextThumbnail . '%title', $category_specific );
elseif ( is_home() || is_archive() || is_search() ) : // navigation links for home, archive, and search pages
if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><span class="prev" title="<?php _e('Previous','generatepress');?>"><?php next_posts_link( __( 'Older posts', 'generatepress' ) ); ?></span></div>
<?php endif;
if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><span class="next" title="<?php _e('Next','generatepress');?>"><?php previous_posts_link( __( 'Newer posts', 'generatepress' ) ); ?></span></div>
<?php endif;
if ( function_exists( 'the_posts_pagination' ) ) {
the_posts_pagination( array(
'mid_size' => apply_filters( 'generate_pagination_mid_size', 1 ),
'prev_text' => __( '&larr; Previous', 'generatepress' ),
'next_text' => __( 'Next &rarr;', 'generatepress' ),
) );
}
do_action('generate_paging_navigation');
endif; ?>
</nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
<?php
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment