_s: Split navigation template tag in context-specific tags.

This commit is contained in:
obenland 2013-11-24 18:28:15 +00:00
parent caff68529f
commit 3f4effb5f0
5 changed files with 51 additions and 43 deletions

View File

@ -87,7 +87,7 @@ get_header(); ?>
<?php endwhile; ?> <?php endwhile; ?>
<?php _s_content_nav( 'nav-below' ); ?> <?php _s_paging_nav(); ?>
<?php else : ?> <?php else : ?>

View File

@ -7,55 +7,63 @@
* @package _s * @package _s
*/ */
if ( ! function_exists( '_s_content_nav' ) ) : if ( ! function_exists( '_s_paging_nav' ) ) :
/** /**
* Display navigation to next/previous pages when applicable. * Display navigation to next/previous set of posts when applicable.
*
* @return void
*/ */
function _s_content_nav( $nav_id ) { function _s_paging_nav() {
global $wp_query, $post; // Don't print empty markup if there's only one page.
if ( $GLOBALS['wp_query']->max_num_pages < 2 ) {
// 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; return;
} }
$nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';
?> ?>
<nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>"> <nav class="navigation paging-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Post navigation', '_s' ); ?></h1> <h1 class="screen-reader-text"><?php _e( 'Posts navigation', '_s' ); ?></h1>
<div class="nav-links">
<?php if ( is_single() ) : // navigation links for single posts ?> <?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', '_s' ) ); ?></div>
<?php endif; ?>
<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '&larr;', 'Previous post link', '_s' ) . '</span> %title' ); ?> <?php if ( get_previous_posts_link() ) : ?>
<?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '&rarr;', 'Next post link', '_s' ) . '</span>' ); ?> <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', '_s' ) ); ?></div>
<?php endif; ?>
<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?> </div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php if ( get_next_posts_link() ) : ?>
<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', '_s' ) ); ?></div>
<?php endif; ?>
<?php if ( get_previous_posts_link() ) : ?>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', '_s' ) ); ?></div>
<?php endif; ?>
<?php endif; ?>
</nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
<?php <?php
} }
endif; // _s_content_nav endif;
if ( ! function_exists( '_s_post_nav' ) ) :
/**
* Display navigation to next/previous post when applicable.
*
* @return void
*/
function _s_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 post-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Post navigation', '_s' ); ?></h1>
<div class="nav-links">
<?php previous_post_link( '%link', _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', '_s' ) ); ?>
<?php next_post_link( '%link', _x( '%title <span class="meta-nav">&rarr;</span>', 'Next post link', '_s' ) ); ?>
</div><!-- .nav-links -->
</nav><!-- .navigation -->
<?php
}
endif;
if ( ! function_exists( '_s_comment' ) ) : if ( ! function_exists( '_s_comment' ) ) :
/** /**

View File

@ -31,7 +31,7 @@ get_header(); ?>
<?php endwhile; ?> <?php endwhile; ?>
<?php _s_content_nav( 'nav-below' ); ?> <?php _s_paging_nav(); ?>
<?php else : ?> <?php else : ?>

View File

@ -23,7 +23,7 @@ get_header(); ?>
<?php endwhile; ?> <?php endwhile; ?>
<?php _s_content_nav( 'nav-below' ); ?> <?php _s_paging_nav(); ?>
<?php else : ?> <?php else : ?>

View File

@ -14,7 +14,7 @@ get_header(); ?>
<?php get_template_part( 'content', 'single' ); ?> <?php get_template_part( 'content', 'single' ); ?>
<?php _s_content_nav( 'nav-below' ); ?> <?php _s_post_nav(); ?>
<?php <?php
// If comments are open or we have at least one comment, load up the comment template // If comments are open or we have at least one comment, load up the comment template