Skip to content

Instantly share code, notes, and snippets.

@atelierbram
Created September 9, 2018 15:35
Show Gist options
  • Save atelierbram/d31e701f238ea9f46ef20d6a733d288a to your computer and use it in GitHub Desktop.
Save atelierbram/d31e701f238ea9f46ef20d6a733d288a to your computer and use it in GitHub Desktop.
WordPress Single Post Nav
<?php
if ( ! function_exists( 'mytheme_post_nav' ) ) :
/**
* Display navigation to next/previous post when applicable.
*
* @return void
*/
function mytheme_post_nav() {
// Don't print empty markup if there's nowhere to navigate.
$previous = ( is_attachment() ) ? get_post( get_post()->post_parent ) : get_adjacent_post( false, '', true );
$next = get_adjacent_post( false, '', false );
if ( ! $next && ! $previous ) {
return;
}
?>
<nav class="navigation posts-navigation">
<div class="post-nav-box">
<h2 class="screen-reader-text"><?php esc_html_e( 'Post navigation', 'mytheme' ); ?></h2>
<div class="nav-links">
<?php
previous_post_link( '<div class="nav-previous"><span class="nav-indicator"><span class="screen-reader-text">' . __( 'previous:', 'mytheme' ) . '</span></span><h5>%link</h5></div>', '%title', TRUE, '' );
next_post_link( '<div class="nav-next"><span class="nav-indicator"><span class="screen-reader-text">' . __( 'next:', 'mytheme' ) . '</span></span><h5>%link</h5></div>', '%title', TRUE, '' );
?>
</div><!-- .nav-links -->
</div><!-- .post-nav-box -->
</nav><!-- .navigation -->
<?php
}
endif; ?>
@atelierbram
Copy link
Author

Above goes in an includes file like: inc/template-tags.php.
In template-parts/content-single.php at the bottom of the page: <?php refugees_post_nav(); ?>

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